0
0
Rustprogramming~10 mins

Propagating errors with ? in Rust - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Propagating errors with ?
Call function that returns Result
Check if Result is Ok or Err
Extract value
Continue with value
The ? operator checks if a Result is Ok or Err. If Ok, it extracts the value. If Err, it returns the error early from the function.
Execution Sample
Rust
fn read_number() -> Result<i32, String> {
    let s = "42";
    let num: i32 = s.parse().map_err(|e| e.to_string())?;
    Ok(num)
}
This function tries to parse a string to a number and uses ? to propagate any parsing error.
Execution Table
StepActionExpressionResultNext Step
1Assign strings = "42"s = "42"Parse string to number
2Parse strings.parse()?Ok(42)Extract 42 and continue
3Return valueOk(num)Ok(42)Function ends successfully
💡 Function ends after returning Ok(42) because no error occurred.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
suninitialized"42""42""42"
numuninitializeduninitialized4242
Key Moments - 3 Insights
What happens if s.parse() returns an Err?
If s.parse() returns Err, the ? operator immediately returns that Err from read_number(), skipping the rest of the function (see execution_table step 2).
Does the code continue after the ? operator if there is an error?
No, the ? operator stops execution and returns the error early, so code after it does not run if an error occurs (see execution_table step 2).
What type must the function return to use the ? operator?
The function must return a Result type compatible with the error type returned by the expression using ?, so the error can be propagated correctly.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of num after step 2?
AErr
B42
C"42"
Duninitialized
💡 Hint
Check the 'Result' column in step 2 and the variable_tracker for num after step 2.
At which step does the function return early if s.parse() fails?
AStep 2
BStep 1
CStep 3
DNever returns early
💡 Hint
Look at the key_moments and execution_table step 2 about error propagation with ?.
If the function's return type was not Result, what would happen when using ? on s.parse()?
ACode would compile and run normally
BRuntime error when ? is used
CCompilation error because ? requires compatible Result return type
DThe ? operator would be ignored
💡 Hint
Refer to key_moments about function return type requirements for using ?.
Concept Snapshot
Use ? to propagate errors in functions returning Result.
If expression is Ok(value), ? extracts value.
If Err(error), ? returns error early.
Function must return compatible Result type.
Simplifies error handling by avoiding match blocks.
Full Transcript
This visual trace shows how the ? operator works in Rust to propagate errors. The function read_number tries to parse a string to an integer. When s.parse()? is called, if parsing succeeds, the value 42 is extracted and assigned to num. If parsing fails, the ? operator returns the error immediately from read_number, skipping the rest of the code. The execution table shows each step: assigning the string, parsing with ?, and returning the Ok result. The variable tracker shows how s and num change. Key moments clarify that ? stops execution on error and requires the function to return a Result type. The quiz tests understanding of variable values, early returns, and type requirements. The snapshot summarizes the ? operator usage for easy reference.