Sitemap
Rustaceans

Insights and Expertise from Rustaceans

Why Rust Doesn’t Have Null: 12 Key Reasons

2 min readOct 1, 2024

--

Press enter or click to view image in full size

🤔 Why doesn’t Rust use null?

Here are 12 simplified reasons:

  1. Rust’s creator, Graydon Hoare, believes null is a major cause of runtime issues in many languages.
  2. Tony Hoare, who introduced the concept of null, admitted it was a design mistake that impacted safety and performance.
  3. Rust focuses on safety and efficiency by avoiding the problems null causes in other languages.
  4. Using null can lead to errors like “null pointer exceptions,” which crash programs, as seen in Java or C++.
  5. Instead of null, Rust uses a type called Option<T> to represent values that may be absent.
  6. Option<T> forces developers to explicitly handle cases where there is no value.
  7. Rust uses a match statement to ensure all possibilities are addressed when dealing with Option , which is just one way to handle the presence or absence of a value.
  8. While Option minimizes null issues, careless use of unwrap() can still cause a panic if unchecked.
  9. Rust provides Result<T, E> for managing errors at runtime, offering a safer way to handle potential failures.
  10. Surveys show Rust developers experience fewer unexpected errors due to the absence of null.
  11. Null-free Rust code is easier to maintain over time.
  12. Avoiding null improves program reliability by reducing issues related to referencing non-existent values.

✨ Summary

Rust avoids null to reduce errors and improve safety and performance. Instead, it uses Option<T> and Result<T, E>, ensuring developers explicitly handle missing values and errors.

Get Neng Apichet’s stories in your inbox

Join Medium for free to get updates from this writer.

References

  1. Tony Hoare. (2009). The Billion Dollar Mistake. https://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare/
  2. Module std::option. https://doc.rust-lang.org/std/option/
  3. Abel Avram. (2012). Interview on Rust, a Systems Programming Language Developed by Mozilla. https://www.infoq.com/news/2012/08/Interview-Rust/
  4. Rust Blog. https://blog.rust-lang.org/

Rustaceans 🚀

Thank you for being a part of the Rustaceans community! Before you go:

  • Show your appreciation with a clap and follow the publication
  • Discover how you can contribute your own insights to Rustaceans
  • Connect with us: X | Rust Bytes Newsletter

--

--