Result and the ? operator
Result<T, E> is Ok or Err. ? returns early on Err.
Example
fn parse(s: &str) -> Result<i32, std::num::ParseIntError> {
let n: i32 = s.parse()?;
Ok(n + 1)
}
fn main() {
println!("{:?} {:?}", parse("42"), parse("x").is_err());
}Result
Ok(43) true
Exercise
Early return on error.
s.parse::<i32>()Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.