love syntactic sugar of the ? operator in rust
Discussion
look at the difference
WITH
fn read_bitcoin_file(file_name: &str) -> Result
let mut bitcoin_file = File::open(file_name)?;
let mut bitcoin_amount = String::new();
bitcoin_file.read_to_string(&mut bitcoin_amount)?;
let amount: f64 = bitcoin_amount.trim().parse().unwrap();
Ok(amount)
}
WITHOUT
fn read_bitcoin_file(file_name: &str) -> Result
let bitcoin_file_result = File::open(file_name);
let mut bitcoin_file = match bitcoin_file_result {
Ok(bf) => bf,
Err(e) => return Err(e),
};
let mut bitcoin_amount = String::new();
let bitcoin_amount = match bitcoin_file.read_to_string(&mut bitcoin_amount) {
Ok(_) => bitcoin_amount,
Err(e) => return Err(e),
};
let bitcoin_amount = bitcoin_amount.trim().parse::
Ok(bitcoin_amount)
}
or how bout we chain method calls?
fn read_bitcoin_file() -> Result
let mut bitcoin_amount = String::new();
File::open("bitcoin_amount_file.txt")?.read_to_string(&mut bitcoin_amount)?;
Ok(bitcoin_amount.trim().parse::
}