0
0
Rustprogramming~10 mins

Result enum in Rust - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Result enum
Start Operation
Operation Succeeds?
NoReturn Err(value)
Yes
Return Ok(value)
Caller Handles Result
Match Ok: Use value
Match Err: Handle error
The Result enum represents success (Ok) or failure (Err). Code returns one, and caller matches to handle each case.
Execution Sample
Rust
fn divide(a: i32, b: i32) -> Result<i32, String> {
    if b == 0 {
        Err(String::from("Divide by zero"))
    } else {
        Ok(a / b)
    }
}
This function divides two numbers and returns Ok with the result or Err with an error message if dividing by zero.
Execution Table
StepInput (a,b)Condition b==0?Returned ResultExplanation
1(10, 2)FalseOk(5)10 divided by 2 is 5, so return Ok(5)
2(10, 0)TrueErr("Divide by zero")Cannot divide by zero, return error
3Caller matches Result-Match Ok or ErrCaller checks if result is Ok or Err to proceed
💡 Function returns either Ok or Err based on divisor; caller must handle both cases.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
a10101010
b2 or 0200 or 2
ResultNoneOk(5)Err("Divide by zero")Ok(5) or Err("Divide by zero")
Key Moments - 2 Insights
Why do we use Result instead of just returning the value?
Result lets us handle errors explicitly. As shown in the execution_table rows 1 and 2, the function returns Ok when successful and Err when there's an error, so the caller can decide what to do.
What happens if we forget to handle the Err case?
If the caller ignores Err, the program might panic or behave unexpectedly. The execution_table row 3 shows the caller must match on Result to safely use the value or handle errors.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what does the function return when b is zero?
AOk(10)
BOk(0)
CErr("Divide by zero")
DNone
💡 Hint
Check step 2 in execution_table where b == 0 condition is True.
At which step does the function return Ok(5)?
AStep 1
BStep 2
CStep 3
DNever
💡 Hint
Look at step 1 in execution_table where b == 2 and result is Ok(5).
If we change the divisor b from 0 to 3, what changes in the variable_tracker?
AVariable a changes
BResult changes from Err to Ok
CNo change in Result
DVariable b becomes zero
💡 Hint
See variable_tracker rows for b and Result values after step 2.
Concept Snapshot
Result enum in Rust:
- Used to return success (Ok) or error (Err)
- Syntax: Result<T, E> where T is success type, E is error type
- Functions return Ok(value) on success
- Return Err(error) on failure
- Caller matches Result to handle both cases
Full Transcript
The Result enum in Rust is a way to handle operations that can succeed or fail. When a function runs, it returns Ok with a value if successful, or Err with an error message if something goes wrong. For example, a divide function returns Ok with the division result if the divisor is not zero, or Err if dividing by zero. The caller then checks the Result using match to decide what to do next. This approach helps avoid crashes and makes error handling clear and safe.