Rust pattern matching with .. Link to heading
Another syntax from Rust that I was not familiar with from other languages: using .. to do pattern matching to ignore remaning fields.
Below are some example usages:
// Define a struct with three fields
struct Person {
name: String,
age: u8,
occupation: String,
}
// Create an instance of Person
let alice = Person {
name: "Alice".to_string(),
age: 25,
occupation: "Software Engineer".to_string(),
};
// Use an if let expression to check the value of alice
if let Person { name, age, .. } = alice {
// Print the name and age fields
println!("Name: {}, Age: {}", name, age);
}
// Define a tuple with four elements
let numbers = (1, 2, 3, 4);
// Use a match expression to check the value of numbers
match numbers {
// Match the first and last elements and ignore the rest
(first, .., last) => println!("First: {}, Last: {}", first, last),
}
This feature is probably not a necessity but can be useful in simplyfing the code a bit.