Enums and Match
🎯 Python's enum module provides enumeration types:
from enum import Enum
class Direction(Enum):
NORTH = "north"
SOUTH = "south"
EAST = "east"
WEST = "west"
def opposite(d: Direction) -> Direction:
match d:
case Direction.NORTH: return Direction.SOUTH
case Direction.SOUTH: return Direction.NORTH
case Direction.EAST: return Direction.WEST
case Direction.WEST: return Direction.EAST
Rust's enums look similar but are more powerful — they can carry data inside variants (we'll see that in later exercises). For now, let's start with the simple case:
enum Season {
Spring,
Summer,
Autumn,
Winter,
}
match with enums
Rust's match pairs naturally with enums. The compiler requires exhaustive matching — you must handle every variant:
fn clothing(season: Season) -> &'static str {
match season {
Season::Spring => "light jacket",
Season::Summer => "shorts",
Season::Autumn => "sweater",
Season::Winter => "heavy coat",
}
}
If you add a new variant to the enum later, the compiler will flag every match that doesn't handle it. Python's structural match doesn't have this guarantee.
&'static str
The return type &'static str means "a string slice that lives for the entire program." String literals like "stop" are compiled into the binary, so they're always available. Think of it as a constant string reference — the Rust equivalent of returning a Python string literal.
Your Task
- Define a
TrafficLightenum with variants:Red,Yellow,Green - Implement
action(light: TrafficLight) -> &'static str: Red→"stop"Yellow→"slow down"Green→"go"
Example
assert_eq!(action(TrafficLight::Red), "stop");
assert_eq!(action(TrafficLight::Yellow), "slow down");
assert_eq!(action(TrafficLight::Green), "go");
Dive deeper: In our Rust Developer Cohort, you'll define a
Tokenenum with variants likeLeftBrace,String(String),Number(f64)— turning raw JSON text into structured, type-safe data.
Further Reading
- The Rust Book — Defining an Enum — enum syntax and variants
- The Rust Book — The match Control Flow — exhaustive pattern matching
Topics