::std::

Module option

Expand

Representing (Valid, Data)-pairs

The Option type represents values which are not always present. In hardware terms it is used to represent a (valid, data)-pair. The compiler ensures that the invalid case is dealt with when an option type is used, preventing accidental reading of invalid values.

Valid values are written as Some(value) while invalid values are written None.

The primary way to interact with Option types is via match statements. For example, To transform the underlying data by adding one, you can write:

match option {
    Some(value) => Some(value + 1),
    None => None
}

In many cases it is better to use the methods that the Option type provides to work with it, as it more clearly signals design intent. For examaple, the transformation above can be written using the map function.

option.map(fn |value| value + 1)

See the documentation for the Option<T> type below for more methods that it implements.

Enums

Option

Represents a (valid, data) pair.

The option type represents a value which may or may not be present, essentially a valid/invalid signal that comes bundled with the data that signal validates.