Question Mark Operator
Write a function parse_and_double that takes a &str, parses it as i32 using ?, doubles it, and returns the result as Result<i32, std::num::ParseIntError>. Test it with "15" and "abc".
Expected output:
text
Double of 15: 30 Failed to parse: invalid digit found in string
Hints:
- Use
text.parse::<i32>()?— the?handles the error automatically - Return
Ok(n * 2)after parsing - In
main, usematchto handle both cases
rust
fn main() {
}