0
0
Rustprogramming~10 mins

Handling errors with match in Rust - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Handling errors with match
Start
Call function that may error
match on Result
Use value
End
This flow shows calling a function that returns a Result, then using match to handle success (Ok) or error (Err) cases.
Execution Sample
Rust
fn divide(a: i32, b: i32) -> Result<i32, String> {
    if b == 0 {
        Err(String::from("Cannot divide by zero"))
    } else {
        Ok(a / b)
    }
}

fn main() {
    match divide(10, 0) {
        Ok(result) => println!("Result: {}", result),
        Err(e) => println!("Error: {}", e),
    }
}
This code tries to divide 10 by 0 and uses match to print the result or error message.
Execution Table
StepActionEvaluationResult
1Call divide(10, 0)b == 0 is trueReturns Err("Cannot divide by zero")
2match on ResultResult is ErrGo to Err branch
3Print error messagePrints "Error: Cannot divide by zero"Output to console
4EndNo more codeProgram ends
💡 Error branch taken because divisor is zero, so program prints error and ends.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
a1010101010
b00000
resultN/AErr("Cannot divide by zero")Err("Cannot divide by zero")Err("Cannot divide by zero")Err("Cannot divide by zero")
eN/AN/A"Cannot divide by zero""Cannot divide by zero""Cannot divide by zero"
Key Moments - 2 Insights
Why does the program go to the Err branch instead of Ok?
Because the divisor b is zero, the divide function returns Err. The match statement checks the Result and takes the Err branch as shown in execution_table step 2.
What happens if the divisor is not zero?
If b is not zero, divide returns Ok with the division result. Then the match takes the Ok branch and prints the result instead of an error.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the Result after Step 1?
AErr("Cannot divide by zero")
BOk(10)
COk(0)
DNone
💡 Hint
Check the 'Result' column in Step 1 of the execution_table.
At which step does the program print the error message?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look for the step where 'Print error message' happens in the execution_table.
If divide(10, 2) was called instead, which branch would match take?
ANeither branch
BErr branch
COk branch
DBoth branches
💡 Hint
Recall that divide returns Ok when divisor is not zero, so match takes Ok branch.
Concept Snapshot
Handling errors with match in Rust:
- Use functions returning Result<T, E>
- Use match to check Result:
  match result {
    Ok(val) => handle success,
    Err(e) => handle error,
  }
- This cleanly separates success and error handling.
Full Transcript
This example shows how Rust uses the match statement to handle errors returned as Result types. The divide function returns Ok if division is possible, or Err if dividing by zero. The main function calls divide and uses match to check if the result is Ok or Err. If Ok, it prints the result; if Err, it prints the error message. The execution table traces each step: calling divide, matching on the result, printing the error, and ending. Variables like a, b, result, and e are tracked through the steps. Key moments clarify why the Err branch is taken when dividing by zero and what happens if the divisor is not zero. The visual quiz tests understanding of the result after calling divide, when the error prints, and which branch match takes for a non-zero divisor.