Structs, Enums & Pattern Matching
Module Overview
Section titled “Module Overview”This module introduces three of Rust’s most important building blocks for modeling data and control flow.
Structs are named collections of fields. They let you group related values under a single type, attach methods via impl blocks, and derive useful traits like Debug and Clone.
Enums model choices. Each variant can optionally carry different data, making them far more expressive than the C-style enums you may know from other languages. Rust’s Option<T> and Result<T, E> are both built-in enums.
match is Rust’s pattern-matching expression. It branches on the shape of a value, binds data from enum variants, and is checked exhaustively by the compiler — every possible case must be handled.
Together, these three features let you define precise, self-documenting data models and write control flow that the compiler verifies for you.
#[derive(Debug)]struct Point { x: f64, y: f64 }
#[derive(Debug)]enum Direction { North, South, East, West }
fn main() { let p = Point { x: 3.0, y: 4.0 }; println!("Point: ({}, {})", p.x, p.y); println!("Debug: {:?}", p);
let directions = [ Direction::North, Direction::South, Direction::East, Direction::West, ];
for dir in &directions { let label = match dir { Direction::North => "North", Direction::South => "South", Direction::East => "East", Direction::West => "West", }; println!("Heading {}", label); }}Compiling…