Zest.

Rust: Option and Result with ?

A short, tested Rust example for option and result with ?. Copy it and adjust the names and paths for your project.

Rust
use std::num::ParseIntError;

fn parse(s: &str) -> Result<i32, ParseIntError> {
    let n: i32 = s.trim().parse()?;   // returns early on error
    Ok(n * 2)
}

let name = user.nickname.unwrap_or("anonymous");
if let Some(x) = maybe { println!("{x}"); }

Find it in Zest

Search for “rust option and result with ?” in Zest and this example appears above the results, with other matches to choose from. Searches stay private.

More Rust examples

All Rust examples