🦀 Rust
Beginner
What is Option<T> in Rust and why does Rust have no null?
Answer
Rust has no null — Tony Hoare called null his "billion-dollar mistake" because null references cause crashes across virtually all languages. Instead, Rust uses Option<T>, an enum with two variants: Some(T) (contains a value) and None (represents the absence of a value). The type system enforces that you cannot use an Option<T> value as if it were T without first checking for None — typically with match, if let, unwrap_or(), or the ? operator. This makes the possibility of absence explicit in the type signature, eliminating null pointer exceptions at compile time. Functions that may not return a value must declare their return type as Option<T>.