Complete the code to destructure the tuple and print the first value.
let tuple = (10, 20); let ([1], _) = tuple; println!("{}", first);
The variable first is used to destructure the first element of the tuple.
Complete the code to destructure the struct and print the name field.
struct Person { name: String, age: u8 }
let person = Person { name: String::from("Alice"), age: 30 };
let Person { [1], age: _ } = person;
println!("{}", name);The field name is destructured from the Person struct to access the name.
Fix the error in the code by completing the pattern to destructure the enum variant.
enum Message {
Quit,
Move { x: i32, y: i32 },
}
let msg = Message::Move { x: 5, y: 10 };
match msg {
Message::Move [1] => println!("x: {}, y: {}", x, y),
_ => (),
}The Move variant has named fields, so the pattern must use curly braces with field names.
Fill both blanks to destructure the nested tuple and print the inner values.
let nested = ((1, 2), (3, 4)); let ([1], [2]) = nested; println!("{} {}", a, b);
The outer tuple is destructured into two inner tuples. The first inner tuple is bound to (a, b), and the second to x (unused here).
Fill all three blanks to destructure a tuple and filter values greater than 5 in a comprehension.
let numbers = vec![(1, 6), (3, 4), (7, 8)]; let filtered: Vec<_> = numbers.iter() .filter(|&&([1], [2])| [3] > 5) .collect();
The tuple is destructured into x and y. The filter checks if y is greater than 5.