RUST can never be NULL


You might not know NULL was born more than a half-century ago, and it is still alive to create the mess for us, but not with RUST.

Although it was considered to be BOOM for the coders, now it’s becoming a BANE for the programmer. With the emerging languages, creators are avoiding the implementation of NULL now.

The inventor of NULL Tony Hoare[2] apologized for inventing it at a software conference called QCon London in 2009. He called it a billion-dollar mistake. He further added:

In 1965, I was designing the first comprehensive type system for references in an object oriented language (ALGOL W). My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn’t resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years.

Many of the times we try to use a null value as a not-null value, we ran into some kind of panic situation causing the termination of our work (code). It is all because of its nature as it is a pervasive property, it’s extremely easy to make this kind of error.

Note: NULL is a value that is currently invalid or absent.

The actual problem is not with the concept, it’s really about the implementation, in the current languages also somewhere or somehow we try to have a null reference, on the other side we also want to be on safer side with it.

Although, Rust does not have NULL, but provides an enum Option which can encode the concept of a value being present or absent.

Here, is a part of Rust Generics, whatever the datatype we will mention in T, Some(T) will have the value of the same datatype.

If you have worked with Java 8 Optional or Scala Option, this is almost same.

We don’t need to explicitly give the datatype to Option if we are assigning a value to some other than NULL/NONE. Rust implicitly check the datatype of the value assigned through Some(T) and assigned it to Option.

let some_value = Some(15);
let some_literal = Some(“Knoldus”);

In the first line of code, it will implicitly check it to be a number where a String in case of the latter, while we need to explicitly provide the data in case of NONE.

let none_number: Option<i32> = None;

I hope you have already been through the blog RUST: Begin the Journey with CODE, where we tried to explain the pattern matching and error handling (panic situation).

To extract out the value of from Option, we need to do the same using pattern matching and error handling. I leave it on you to extract the value and use it with your code. In case you don’t have any idea about it please go through the RUST: Begin the Journey with CODE.

1. The Rust Programming Language
2. Tony Hoare – Wikipedia



Written by 

Sourabh Verma is a Software Consultant with experience of more than 2 years. He has a deep understanding of Java and familiar with Spring Framework, JPA, Hibernate, JavaScript, Spark, Scala, AngularJS, Angular 4. He is an amazing team player with self-learning skills and a self-motivated professional. He loves to play with Real-time problems, Big data, Cloud computing, Agile Methodology and Open Source Technology. He is eager to learn new technologies and loves to write blogs and explore nature.

Leave a Reply

%d bloggers like this: