Hi I'm currently reading The Rust Programming Language book. In error handling chapter I found this code works well:
fn read_username_from_file() -> Result<String, io::Error> {
let f = File::open("hello.txt");
let mut f = match f {
Ok(file) => file,
Err(e) => return Err(e),
};
let mut s = String::new();
match f.read_to_string(&mut s) {
Ok(_) => Ok(s),
Err(e) => Err(e),
}
}
However, after rewritting using ? operator, the code only works in this way:
fn read_username_from_file() -> Result<String, io::Error> {
let mut f = File::open("hello.txt")?;
let mut s = String::new();
f.read_to_string(&mut s)?;
Ok(s)
}
Please note that f now become mutable! In the first version f is immutable. The definition of read_to_string is fn read_to_string(&mut self, buf: &mut String) -> Result<usize>, which requires a mutable self reference. But why the first version works without an compile time error here? f (self) is not mutable here!