How to Handle NULL Values from Databases in Rust

Use the Option<T> enum and pattern matching to safely handle missing values instead of NULL in Rust.

Rust does not have NULL; use the Option<T> enum to represent values that might be missing. Match on the Some variant to use the value or handle None to avoid crashes.

fn get_user_id() -> Option<i32> {
    // Returns Some(id) if found, None if not
    Some(42)
}

fn main() {
    match get_user_id() {
        Some(id) => println!("User ID: {}", id),
        None => println!("User not found"),
    }
}