Save $1 billion with the Option type

Fehmi Can Saglam
4 min readJan 12, 2022

Tony Hoare, who introduced null references in 1965, described them as “The Billion Dollar Mistake” in this speech in 2009. No matter how much experience you have, I believe that you have had problems caused by null pointers in any programming language such as NullPointerException in Java, NoMethodError in Ruby or a segfault in C. Since these issues occur at runtime, the odds are that you were paged in the middle of the night as the on-call engineer at some point in your career.

My brief history with the Option type

To prevent dereferencing null pointers at compile time, a few languages offer an Option type which wraps another value to express the presence or absence of it. Option already existed in Scala when I first started using it in 2012. It was such a relief for me after years of having issues in Java. With the release of Java 8 in 2014, Java introduced Optional as well, even if it had limited capabilities in terms of composibility compared to Scala’s Option. I’m so happy that Rust includes an Option type which has almost the same capabilities as Scala. In the rest of this article, I will talk about Rust’s Option type.

enum Option<T>

Rust does not allow null references. In languages that allow null references, the compiler doesn’t enforce checking them. However, when using the Option type the compiler makes it mandatory to perform this check .

--

--

Fehmi Can Saglam

I’m a software engineer based in Berlin. I write mostly about programming and living healthy.