Back to questions
JuniorTheoryCommon
What happens when this program is built and run?
fn main() {
let n = "x".parse::<i32>().unwrap();
println!("{n}");
}It compiles and panics
- Expecting
0, as fromunwrap_or(0):unwraphas no fallback, and onErrit panics withcalled `Result::unwrap()` on an `Err` value: ParseIntError { kind: InvalidDigit }. - Expecting a compile error because
xis plainly not a number: the compiler checks types, andparsereads the text only at run time.
- How would the panic message change if
unwrapwere replaced withexpect? - With which exit code does the program end after this panic?