liveBook · Manning

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.

How 4 Manning books explain it

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.

Read how Idiomatic Rust explains question mark operator →

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

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.