Files
100-exercises-to-learn-rust/book/src/05_ticket_v2/05_nullability.md
2024-05-28 11:04:19 +02:00

2.2 KiB
Raw Blame History

Nullability

Our implementation of the assigned method is fairly blunt: panicking for to-do and done tickets is far from ideal.
We can do better using Rusts Option type.

Option

Option is a Rust type that represents nullable values.
It is an enum, defined in Rusts standard library:

enum Option<T> {
    Some(T),
    None,
}

Option encodes the idea that a value might be present (Some(T)) or absent (None).
It also forces you to explicitly handle both cases. Youll get a compiler error if you are working with a nullable value and you forget to handle the None case.
This is a significant improvement over “implicit” nullability in other languages, where you can forget to check for null and thus trigger a runtime error.

Options definition

Options definition uses a Rust construct that you havent seen before: tuple-like variants.

Tuple-like variants

Option has two variants: Some(T) and None.
Some is a tuple-like variant: its a variant that holds unnamed fields.

Tuple-like variants are often used when there is a single field to store, especially when were looking at a “wrapper” type like Option.

Tuple-like structs

Theyre not specific to enums—you can define tuple-like structs too:

struct Point(i32, i32);

You can then access the two fields of a Point instance using their positional index:

let point = Point(3, 4);
let x = point.0;
let y = point.1;

Tuples

Its weird to say that something is tuple-like when we havent seen tuples yet!
Tuples are another example of a primitive Rust type. They group together a fixed number of values with (potentially different) types:

// Two values, same type
let first: (i32, i32) = (3, 4);
// Three values, different types
let second: (i32, u32, u8) = (-42, 3, 8);

The syntax is simple: you list the types of the values between parentheses, separated by commas. You can access the fields of a tuple using the dot notation and the field index:

assert_eq!(second.0, -42);
assert_eq!(second.1, 3);
assert_eq!(second.2, 8);

Tuples are a convenient way of grouping values together when you cant be bothered to define a dedicated struct type.