Lesson 15 of 50 · rust
Pattern Matching
Duration: 18 minutes
Lesson 15 of 50 · rust
Duration: 18 minutes
match is exhaustive—all possibilities must be handled:\n\nrust\nmatch value {\n pattern1 => result1,\n pattern2 => result2,\n _ => default_result, // Catch-all\n}\n\n\n## Matching Enums with Data\n\nrust\nenum Message {\n Quit,\n Move { x: i32, y: i32 },\n Write(String),\n ChangeColor(i32, i32, i32),\n}\n\nfn process(msg: Message) {\n match msg {\n Message::Quit => println!(\"Quit\"),\n Message::Move { x, y } => {\n println!(\"Move to ({}, {})\", x, y);\n }\n Message::Write(text) => println!(\"Text: {}\", text),\n Message::ChangeColor(r, g, b) => {\n println!(\"Color: RGB({}, {}, {})\", r, g, b);\n }\n }\n}\n\n\n## Matching Tuples\n\nrust\nfn describe_tuple(t: (i32, String, bool)) {\n match t {\n (0, s, _) => println!(\"First is zero, string: {}\", s),\n (n, _, true) => println!(\"First: {}, third is true\", n),\n (n, s, f) => println!(\"All parts: {}, {}, {}\", n, s, f),\n }\n}\n\n\n## Matching Structs\n\nrust\nstruct Point {\n x: i32,\n y: i32,\n}\n\nfn describe_point(p: Point) {\n match p {\n Point { x: 0, y: 0 } => println!(\"Origin\"),\n Point { x, y: 0 } => println!(\"On x-axis at {}\", x),\n Point { x: 0, y } => println!(\"On y-axis at {}\", y),\n Point { x, y } => println!(\"At ({}, {})\", x, y),\n }\n}\n\n\n## Catch-All with Underscore\n\nrust\nfn demo(value: i32) {\n match value {\n 1 => println!(\"One\"),\n 3 => println!(\"Three\"),\n 5 => println!(\"Five\"),\n _ => println!(\"Something else\"), // Catch-all\n }\n}\n\n\n## If Let for Simple Cases\n\nWhen matching only one pattern:\n\nrust\nlet config_max = Some(3u8);\n\n// Verbose match\nmatch config_max {\n Some(max) => println!(\"Max: {}\", max),\n _ => (),\n}\n\n// Concise if let\nif let Some(max) = config_max {\n println!(\"Max: {}\", max);\n}\n\n\n## While Let\n\nContinue looping while a pattern matches:\n\nrust\nlet mut stack = vec![1, 2, 3];\n\nwhile let Some(top) = stack.pop() {\n println!(\"Popped: {}\", top);\n}\n\n\n## Guard Conditions\n\nAdd conditions to patterns:\n\nrust\nfn describe_value(value: i32) {\n match value {\n n if n < 0 => println!(\"Negative: {}\", n),\n n if n == 0 => println!(\"Zero\"),\n n if n % 2 == 0 => println!(\"Even: {}\", n),\n _ => println!(\"Odd positive\"),\n }\n}\n\n\n## Combining Patterns\n\nMultiple patterns in one arm:\n\nrust\nfn demo(value: i32) {\n match value {\n 1 | 3 | 5 => println!(\"Odd single digit\"),\n 2 | 4 | 6 => println!(\"Even single digit\"),\n _ => println!(\"Something else\"),\n }\n}\n\n\n## Range Patterns\n\nrust\nfn demo(value: i32) {\n match value {\n 1..=5 => println!(\"One through five\"),\n 6..=10 => println!(\"Six through ten\"),\n _ => println!(\"Other\"),\n }\n}\n\n\n## Practical Example\n\nrust\nstruct User {\n name: String,\n age: u32,\n}\n\nfn categorize_user(user: Option<User>) {\n match user {\n Some(User { name, age }) if age >= 18 => {\n println!(\"{} is an adult\", name);\n }\n Some(User { name, age }) => {\n println!(\"{} is a minor ({})\", name, age);\n }\n None => println!(\"No user\"),\n }\n}\n\n\n## Pattern Matching Benefits\n\n1. Exhaustiveness: Compiler ensures all cases handled\n2. Safety: Can't access wrong fields by mistake\n3. Expressiveness: Complex logic becomes readable\n4. Type safety: Pattern matching respects types