What is a 'Result'?
Part of the Logic & Flow section of Coddy's Rust journey — lesson 42 of 66.
While Option handles the absence of values, Rust provides another enum for a different but equally important scenario: operations that can fail. Meet Result<T, E> - Rust's primary tool for handling recoverable errors.
The Result enum has two variants that represent the outcome of an operation:
enum Result<T, E> {
Ok(T), // Success - contains the successful result
Err(E), // Failure - contains error information
}When an operation succeeds, it returns Ok(value) containing the successful result. When it fails, it returns Err(error) containing information about what went wrong. This explicit handling of success and failure cases makes Rust programs more reliable and predictable.
You'll encounter Result everywhere in Rust - from file operations that might fail due to missing files, to network requests that could timeout, to parsing operations that might receive invalid input. Unlike languages that use exceptions, Rust forces you to acknowledge that operations can fail and handle both outcomes explicitly.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
Cheat sheet
The Result<T, E> enum handles operations that can fail:
enum Result<T, E> {
Ok(T), // Success - contains the successful result
Err(E), // Failure - contains error information
}When an operation succeeds, it returns Ok(value). When it fails, it returns Err(error).
Result is used for recoverable errors like file operations, network requests, and parsing operations. Rust requires explicit handling of both success and failure cases.
Try it yourself
This lesson doesn't include a code challenge.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Advanced Control Flow
The 'match' ExpressionMatching Multiple ValuesMatching RangesThe 'if let' ExpressionLoops as ExpressionsRecap - Simple Command Parser4Grouping Data with Structs
What is a Struct?Structs OverviewAccessing Struct FieldsMutable StructsStructs as Function ParametersTuple StructsRecap - Create a Book Struct7Handling Errors with 'Result'
What is a 'Result'?Using 'match' with 'Result'is_ok() and is_err()Shortcuts: unwrap and expectThe Question Mark Operator '?'Parsing Strings to NumbersRecap - Safe Division Function10Closures & Anonymous Functions
What is a Closure?Defining a Simple ClosureClosures with ParametersCapturing the EnvironmentRecap - Simple Adder Closure2Introduction to Vectors
What is a Vector?Creating a VectorAdding Elements with pushAccessing Vector ElementsIterating Over a VectorMutable IterationRemoving ElementsRecap - Basic Score Tracker5Key-Value Pairs with Hash Maps
What is a Hash Map?Creating a Hash MapInserting Key-Value PairsAccessing ValuesIterating Over a Hash MapUpdating a ValueRemoving a PairRecap - Word Counter8Project: Simple Item Inventory
Project SetupAdding an ItemChecking StockSelling an ItemPutting it all together