operator
Rust ?: propagate an error
Inside a compatible Result-returning function, `?` extracts an Ok value or returns early on Err, converting the error when required. It also works with Option in compatible Option-returning contexts. Expected output (not executed): true true Why: The first string parses; the second returns the error early rather than panicking. Common mistake: `?` is not an unconditional unwrap and cannot be used in every function return context.
Formal Syntax & Structure
rust syntaxrust
fallible_expression?Working Example
Example Snippetrust
fn main() { fn parse_count(text: &str) -> Result<u32, std::num::ParseIntError> { let count = text.parse::<u32>()?; Ok(count) } println!("{}", parse_count("12").is_ok()); println!("{}", parse_count("none").is_err());}