Back to questions
JuniorTheoryCommon
What does the program print?
fn main() {
let n: i32 = "x".parse().unwrap_or(0);
println!("{n}");
}0
- Expecting a panic because of
unwrapin the name:unwrap_orreturns its argument onErrand never panics. - Expecting a compile error without
parse::<i32>(): the annotationlet n: i32already tells the compiler which type to parse into.
- What would the program print if
xwere replaced with7? - When would you pick
unwrap_or_elseoverunwrap_or?