RUST Question mark

fn main() {
use std::fs::File;
use std::io::prelude::*;

fn foo() -> std::io::Result<()> {
     let mut file = File::create("foo.txt")?; //здесь
     file.write_all(b"Hello, world!")?; //здесь 
     Ok(())
   }
}

Why do I need a question mark ?

 4
Author: new, 2017-12-31

1 answers

expression? - this is an abbreviated entry for try!(expression). Description of the try! macro from the Rust manual.

In short. If expression returns the value Err(err), then try! (or ?) immediately exits the function: return Err(err.into()). If the same value is expression - Ok(v), then the macro returns v and execution continues.

 7
Author: red75prim, 2017-12-31 04:10:08