Onebite.dev

Developer tips & trick, one bite at a time

The Option Enum in Rust, null alternative

Rust didn’t have a null value. But there is Enum Option that can implement null behavior.

Enum Option could represents a value: it could be something, or it could be nothing.

This is how it looks like

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

T itself is a generic type parameter.

Here is concrete example of Enum Option:

fn main() {
    let some_number = Some(5);
    let some_string = Some("a string");

    let absent_number: Option<i32> = None;
}

Notice, if we want to assign None, we have to declare specific type of Option




This is a post in the start learning Rust series.



🏊🏾‍♀️ Back Home