Rust: Pattern Matching and the Option Type
rusttype-systemsnull-safetypattern-matching
Abstraction: Rust Option type eliminates null pointer bugs via exhaustive pattern matching
Key points:
- Rust has no null pointers; absence of value is represented by
Option<T>, an enum ofSome(T)orNone. - Pattern matching on
Optionis exhaustive — the compiler rejects code that does not handle theNonecase, preventing segfaults that C/C++ allow. - C.A.R. Hoare called the null pointer his "billion-dollar mistake" (invented 1965 in ALGOL W).
- Rust's
if {} else {}blocks are expressions, not statements; the last expression without a semicolon is the return value. - IEEE 754 floats encode special values (±Infinity, NaN) for similar reasons — representing the absence of a meaningful numeric result.
Connections: Rust · Option Type · Pattern Matching · Null Safety · Type Systems
Source: http://nickdesaulniers.github.io/blog/2013/05/07/rust-pattern-matching-and-the-option-type/