October 2018
Beginner
180 pages
4h 48m
English
You might have noticed in our previous example that we did not handle the case where the function returned an error value. In part, that's because handling that situation with if let is a little bit awkward. We could do this:
if let Ok(x) = might_fail(39) { println!("Odd succeeded, name is {}", x.name);}else if let Err(x) = might_fail(39) { println!("Odd failed, message is '{}'", x);}
But that runs the function twice when it doesn't have to, so it's inefficient. We could fix that by doing this:
let result = might_fail(39);if let Ok(x) = result { println!("Odd succeeded, name is {}", x.name);}else if let Err(x) = result { println!("Odd failed, message is '{}'", x);}
That's better, but variables ...
Read now
Unlock full access