Back to questions
JuniorTheoryCommon
What happens when this program is built and run?
fn main() {
let n: i32 = "7".parse()?;
println!("{n}");
}It does not compile
- Expecting
7because the string is a valid number:?needs a function that returnsResultorOption, whatever value turns up at run time. - Expecting
?to panic onErrlikeunwrap: it returns the error from the function, so the function's return type must be able to hold it.
- How would you change the signature of
mainso that the program compiles? - What does such a
mainprint when the string is not a number?