Consider the following example
use std::fs::File;
use std::io::{BufRead, BufReader, Result};
fn main() {
let file = File::open("myfile.txt")?; // This doesn't work
let file = File::open("myfile.txt").unwrap(); // this works
for line in BufReader::new(file).lines() {
println!("{}", line.unwrap());
}
}
using rustc 1.33.0 (2aa4c46cf 2019-02-28)
I am trying to use ?
to substitute unwrap, but it seems like I always get ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot use the
?operator in a function that returns
()
, can someone point me a direction?
Thanks