From the course: Rust Essential Training

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Option<T> enum

Option<T> enum - Rust Tutorial

From the course: Rust Essential Training

Start my 1-month free trial

Option<T> enum

- In addition to working with values that represent something. In programming we'll often also need to represent the concept that a value could be nothing. Many programming languages use a null value to indicate when there is no value. In those languages, variables can either be null or not null meaning they do have a value. It's a simple and straightforward system. But problems arise when you try to use a null value in something that requires a not null value. It's easy to do by accident and it's a common source of runtime errors. That's not safe. And therefore Rust does not have a null value in the traditional sense like many other languages. Instead, Rust implements the concept using a generic Enum named option, which can be one of two variants. If it's the sum variant that indicates it's not null and does have a value which is stored Enum. Otherwise, if the Enum is the none variant that indicates it does not have…

Contents