Skip to content
Justice Tech School

Option

Option<T> is Some(value) or None. It replaces null.

Example
fn main() {
    let a: Option<i32> = Some(5);
    let b: Option<i32> = None;
    println!("{:?} {:?}", a.map(|x| x * 2), b.unwrap_or(0));
    println!("{}", a.is_some());
}

Result

Some(10) 0
true

Exercise

No value.

let x: Option<i32> = ;

Quiz Test

What replaces null in Rust?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.