From the course: Rust Essential Training

Unlock this course with a free trial

Join today to access over 25,300 courses taught by industry experts.

Option<T> enum

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