Structs, enums and match in Rust
Learn Rust · Beginner · 12 min · After Borrowing and references in Rust
How do you model your own data in Rust?
1Guess
enum Light { Red, Yellow, Green, } fn main() { for light in [Light::Red, Light::Yellow, Light::Green] { match light { Light::Red => println!("stop"), Light::Green => println!("go"), _ => println!("wait"), } } }2Read
Structs and methods
A struct groups named fields:
struct Rectangle { width: u32, height: u32 }. You make an instance by giving every field a value and read a field with a dot. Methods live in animplblock. A method whose first parameter is&selfborrows the instance, so you callrect.area()and still ownrectafterwards.3Code
A struct with a method
1 / 2#[derive(Debug)] struct Rectangle { width: u32, height: u32, } impl Rectangle { fn area(&self) -> u32 { self.width * self.height } } fn main() { let rect = Rectangle { width: 30, height: 50, }; println!("{:?}", rect); println!("area is {}", rect.area()); }Read the program. What will it print?
4Read
Enums, Option and match
An enum is a type with a fixed set of variants, and a variant can hold data. Rust has no null. A value that may be missing has the type
Option<T>:Some(value)orNone.matchcompares a value with patterns, and it is exhaustive: you must cover every variant, or use_for the rest.5Code
An enum with data, and an Option
1 / 2enum Shape { Square(f64), Rect { w: f64, h: f64 }, } fn area(shape: &Shape) -> f64 { match shape { Shape::Square(side) => side * side, Shape::Rect { w, h } => w * h, } } fn main() { println!("{}", area(&Shape::Square(3.0))); println!("{}", area(&Shape::Rect { w: 2.0, h: 5.0 })); let found: Option<u32> = Some(7); if let Some(n) = found { println!("found {n}"); } }Read the program. What will it print?
6Fix
enum Coin { Penny, Nickel, Dime, } fn cents(coin: &Coin) -> u32 { match coin { Coin::Penny => 1, Coin::Nickel => 5, } } fn main() { println!("{}", cents(&Coin::Penny)); println!("{}", cents(&Coin::Nickel)); println!("{}", cents(&Coin::Dime)); }7Say it back
8Mission · 10 min
Define an enum with three variants, one of them holding a number, and a function that matches on it. Add a struct with an impl block and a method, and print both from main. Then remove one match arm and read the error.
cargo new shapes cd shapes cargo run
Free AI coding tools to practise with
- AgentRouter Free Credits
Free AI coding credits: $25 per daily check-in and $50 per invite
No cardVerified
- Cline Free Models
Try select models free in Cline via rotating promotions with a usage quota
No cardVerified
Common questions
- How do I define a struct in Rust?
- Write the keyword struct, a name and a list of named fields with their types in braces, for example struct Rectangle { width: u32, height: u32 }. To make a value, give every field a value, and read a field with a dot. Methods go in a separate impl block, with &self as the first parameter.
- What is Option in Rust and why is there no null?
- Rust has no null value. When a value may be missing, its type is Option<T>, an enum with two variants: Some(value) and None. Because it is a different type from T, the compiler makes you handle the None case before you can use the value, so you cannot forget it by accident.
- Why must a match be exhaustive in Rust?
- Every match must cover all possible values of what it matches, or the program does not compile. The compiler even names the missing pattern. This means a forgotten case becomes a compile error rather than a bug at run time. If you only care about some cases, a final _ arm covers all the rest.
Sources
- https://doc.rust-lang.org/book/ch05-01-defining-structs.html (read 2026-12-03)
- https://doc.rust-lang.org/book/ch06-01-defining-an-enum.html (read 2026-12-03)
- https://doc.rust-lang.org/book/ch06-02-match.html (read 2026-12-03)
Measured with rustc 1.100.1 (play.rust-lang.org, stable, edition 2024) on 2026-12-03
Published Dec 3, 2026 · Updated Dec 3, 2026