Question mark operator
The question mark operator is Rust postfix syntax that propagates an error or absence by returning early and otherwise produces the successful value.
Rust Servers, Services, and Apps
The Rust question-mark operator unwraps successful results or propagates errors or absence from the enclosing function.
- How it is used
- Replaces panic-prone database-fetch unwrapping with error propagation to a web-service handler.
- How deep it goes
- Works through match replacement, return-error compatibility, conversion, and async database calls without examining operator internals.
- Examples
- Uses string-to-integer squaring, course and tutor database queries, and web-server binding.
- What you can do after
- Apply it via SQLx and Actix Web to propagate database failures through Result-returning functions toward HTTP error responses.
Read how Rust Servers, Services, and Apps explains question mark operator →
Learn Rust in a Month of Lunches
The question mark operator is Rust syntax that propagates an error from a Result or absence from an Option.
- How it is used
- Uses `?` to keep successful code moving while returning parsing, file, or application errors to the caller.
- How deep it goes
- Explains `Ok` and `Err` behavior through a worked parsing function and equivalent `match`, but defers combining different error types.
- Examples
- Shows string-to-integer parsing, a parsing loop that stops on error, and application refactoring that replaces `.unwrap()`.
- What you can do after
- Practice/perform error propagation in Rust functions returning `Result`, including replacing `.unwrap()` with `?` where returning errors is appropriate.
Read how Learn Rust in a Month of Lunches explains question mark operator →
Idiomatic Rust
The ? operator is a Rust postfix operator that propagates a Result error or an absent Option value by returning early from the enclosing function.
- How it is used
- Propagates failures while keeping chains of fallible Rust calls readable instead of nesting matches.
- How deep it goes
- Explains early return, compatible enclosing return types, and error conversion through `From`, stopping short of broader error-type design.
- Examples
- Uses file creation and writing, a custom-error conversion, and an nth-line reader with missing-line handling.
- What you can do after
- Apply it to propagate compatible `Result` or `Option` failures while reserving explicit handling for errors requiring local action.
Code Like a Pro in Rust
The question mark operator is Rust syntax that propagates an error from a Result or Option expression and otherwise produces its successful value.
- How it is used
- Keeps fallible functions concise by returning errors early instead of repeating error-handling code.
- How deep it goes
- Shows two propagation sites and the `From` conversion they require, while leaving function-specific error handling to explicit code.
- Examples
- Uses a file-reading function that opens a file, reads it into a string, and converts I/O errors to a custom error.
- What you can do after
- Practice/perform early error propagation in fallible functions, adding `From` conversions when their error types differ.
Read how Code Like a Pro in Rust explains question mark operator →
Related concepts
- unwrapalternative to · unwrap is a Rust method that extracts a value from an Option or Result and panics when it is absent or represents an error.
- result typeuses · A type that represents an operation’s outcome as either a successful value or a failure value.
- exceptionused by · An exception is a runtime signal or object representing an abnormal condition that interrupts ordinary program flow and may be handled or propagated.
- pattern matchingequivalent to · Pattern matching is the process of comparing data or values with specified or learned patterns to identify correspondence, conformity, or a matching case.
- early returnproduces · An early return is a control-flow technique in which a function exits before reaching its normal completion point.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.



