Skip to content

Latest commit

 

History

History
64 lines (47 loc) · 1.15 KB

File metadata and controls

64 lines (47 loc) · 1.15 KB

Patterns and Matching1

Note: There is no program included in this chapter, as it's notes only.

Patterns are a special syntax in Rust for matching the structure of types:

  • Literals
  • Destructured arrays, enums, structs, or tuples
  • Variables
  • Wildcards
  • Placeholders

TIP: x, (a, 3), and Some(Color::Red) are all example patterns.

All the Places Patterns Can Be Used

The most common is match:

match x {
    None => None,
    Some(i) => Some(i + 1),
}

The next is a conditional if let expression:

// Similar to above, but the compiler will not check for exhaustiveness.
if let Some(i) = x {
  // ...
}

And while let and for:

while let Some(top) = stack.pop() {
  // ...
}
for (index, value) in v.iter().enumerate() {
  // ...
}

Function parameters can also be patterns:

fn print_coordinates(&(x, y): &(i32, i32)) {
    println!("Current location: ({}, {})", x, y);
}

fn main() {
    let point = (3, 5);
    print_coordinates(&point);
}

See also: https://doc.rust-lang.org/book/ch18-03-pattern-syntax.html.

Footnotes

  1. Source: https://doc.rust-lang.org/book/ch18-00-patterns.html