Skip to content
← All examples

Parse an age

Rust Exceptions

the ? operator

Flowchart (ISO 5807)

YesNoYesNoStartInput sage = s.trim().parse()Error?Return the errorEndage > 150Return Ok(150)EndReturn Ok(age)EndFigure 1 — parse_age

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)
}