liveBook · Manning

Question mark operator

The Rust question mark operator propagates an error or absence by returning early from the enclosing function, and otherwise yields 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 concise early returns to propagate errors instead of repeatedly matching results or calling unwrap.
How deep it goes
Explains success and early-return paths through a match equivalent, chaining, and return types; mentions Option briefly.
Examples
Walks through integer parsing and a loop that stops at invalid input, then applies propagation to file operations and application cleanup.
What you can do after
Apply it in Rust functions to propagate parsing errors and continue only when operations succeed.

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
Uses concise error propagation in fallible functions and connects Result-returning functions to testable code.
How deep it goes
Explains early returns and error conversion through a worked file-reading implementation, while noting that function-specific handling may need another approach.
Examples
Demonstrates opening a file, reading its contents, and converting an I/O error into a crate-specific error.
What you can do after
Apply the operator with Rust’s standard I/O library to propagate errors from fallible operations.

Read how Code Like a Pro in Rust explains question mark operator →

Also covered in

What to know first

Related concepts

Questions about question mark operator

What is question mark operator?

The Rust question mark operator propagates an error or absence by returning early from the enclosing function, and otherwise yields the successful value.

Which Manning books explain question mark operator?

Rust Servers, Services, and Apps, Learn Rust in a Month of Lunches, Idiomatic Rust and Code Like a Pro in Rust.

What should I know before learning question mark operator?

Manning books that teach question mark operator usually introduce result type, From trait, option and crate first.

What is the difference between question mark operator and unwrap?

The Rust question mark operator propagates an error or absence by returning early from the enclosing function, and otherwise yields the successful value. In Rust, unwrap extracts the value from an Option or successful Result, panicking if the Option is None or the Result is an error.

What is question mark operator a kind of?

Question mark operator is a kind of operator. An operator is a symbol, keyword, or language construct that specifies an operation on one or more operands.

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.