Superteam Academy
|bySuperteam BrasilSuperteam Brasil
Superteam Academy

The decentralized learning platform for Solana developers.

Resources

  • Courses
  • Leaderboard
  • Community

Support

  • Documentation
  • API Reference
  • Community

Legal

  • Terms of Service
  • Privacy Policy
  • Cookie Policy

Follow Us

  • Twitter / X
  • Discord
  • GitHub

Stay in the Loop

Get weekly updates on new courses, Solana ecosystem news, and learning tips.

Powered bySuperteam BrasilSuperteam Brasil

Β© 2026 Superteam Academy. All rights reserved.

Rust Error Handling Patterns

Option and Result Types

Result and Option / Option and Result Types
30 XP

Option and Result Types

No Null, No Exceptions

Rust doesn't have null or exceptions. Instead it uses two enums:

Option<T> β€” Maybe a Value

Rust
enum Option<T> {
    Some(T),
    None,
}

Use when a value might not exist:

Rust
fn find_user(id: u64) -> Option<String> {
    if id == 1 {
        Some(String::from("Alice"))
    } else {
        None
    }
}

match find_user(1) {
    Some(name) => println!("Found: {name}"),
    None => println!("Not found"),
}

Result<T, E> β€” Success or Failure

Rust
enum Result<T, E> {
    Ok(T),
    Err(E),
}

Use when an operation can fail:

Rust
use std::num::ParseIntError;

fn parse_age(input: &str) -> Result<u8, ParseIntError> {
    input.parse::<u8>()
}

The ? Operator

Propagate errors without verbose match blocks:

Rust
fn get_user_age(input: &str) -> Result<u8, ParseIntError> {
    let age = input.parse::<u8>()?; // returns Err early if parse fails
    Ok(age)
}

Useful Combinators

Rust
// Option
let name = find_user(1).unwrap_or(String::from("Unknown"));
let upper = find_user(1).map(|n| n.to_uppercase());

// Result
let age = parse_age("25").unwrap_or(0);
let doubled = parse_age("25").map(|a| a * 2);

Why This Matters for Solana

Anchor programs return Result<()>. Every instruction handler uses ? to propagate errors. Understanding this pattern is essential.

You need to enroll in this course before you can mark lessons as complete.
1 / 2