Menu
Coddy logo textTech

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.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

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.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow