April 2026
Intermediate
631 pages
16h 20m
English
Following our discussion on enums, we now shift our focus to the Option type, a powerful enum provided by Rust that represents the possibility of a value being present or absent. In this section, we’ll explore why the Option type is useful, examine its definition, and dive into pattern matching with Option. We’ll also show you how to use if let for more concise handling of optional values.
Let’s imagine we are implementing the code shown in Listing 5.22.
struct Student { name: String, grade: u32,}fn main() { let student_db = vec![ Student { name: String::from("Alice"), grade: Some(95), }, Student { name: String::from("Bob"), grade: Some(87), }, ];}
Listing 5.22 Program for Simulating a Student Database ...
Read now
Unlock full access