Back to questions
JuniorTheoryCommon
What does the program print?
fn main() {
match "42a".parse::<i32>() {
Ok(n) => println!("{n}"),
Err(_) => println!("bad"),
}
}bad
- Expecting
42, as from C'satoior JavaScript'sparseInt:parseaccepts only a string that is a number as a whole, so42agivesErr. - Expecting a panic, like an exception in other languages: a failed
parsereturnsErr, and the program panics only if it callsunwrapon it.
- How would you change the
Errarm to print why the parse failed? - Why does a line read with
read_linefail to parse until you calltrim()?