Error
The ?
operator at the end of a Rust function is known as the "try operator" or "question mark operator." It's a powerful feature introduced in Rust 1.38 that simplifies error handling in functions. Let's dive into its usage and benefits.
Understanding the ?
Operator
?
OperatorThe ?
operator is used to propagate errors up the call stack automatically. When used at the end of a function, it unwraps the result of the last expression in the function and returns early if there's an error.
How It Works
If the last expression evaluates to
Ok(value)
, the function continues executing normally.If the last expression evaluates to
Err(error)
, the function immediately returns with that error.The
?
operator only works on types that implement theTry
trait, such asResult
andOption
.
Last updated