Parse an age
Rust Exceptions
the ? operator
Flowchart (ISO 5807)
Source code
fn parse_age(s: &str) -> Result<u32, std::num::ParseIntError> {
let age: u32 = s.trim().parse()?;
if age > 150 {
return Ok(150);
}
Ok(age)
} the ? operator
fn parse_age(s: &str) -> Result<u32, std::num::ParseIntError> {
let age: u32 = s.trim().parse()?;
if age > 150 {
return Ok(150);
}
Ok(age)
}