0

Rust – How to check for NULL values

Last Updated on

NULL not supported

Rust does not support NULL unlike some other modern programming languages like Java. Actually, it does not have NULL or null keyword. However, it provides an enum Option used in similar way to Java 8 Optional.

Rust Enforces Variable Initialization

Rust enforces variable initialization both for “global” and “local” variables.

These codes result to the following two separate error messages.

Compile Error 1

Compile Error 2

Using Enum Option

Below is a short code snippet demonstrating how to use the enum Option.

Output:

Tested with Rust 1.37.0.

Summary

Since NULL or null is not supported, there is no need to check for NULL values in Rust.

Karl San Gabriel

Karl San Gabriel

Professional Software Developer