21.16 Patterns in for Loops and Function Parameters

21.16.1 for Loops

A for loop’s header can destructure each item from the iterator:

fn main() {
    let data = vec!["apple", "banana", "cherry"];
    for (index, fruit) in data.iter().enumerate() {
        println!("{}: {}", index, fruit);
    }
}

Here, (index, fruit) destructures the (usize, &&str) tuple from .enumerate().

21.16.2 Function Parameters

Function parameters can also be patterns:

fn sum_pair((a, b): (i32, i32)) -> i32 {
    a + b
}

fn main() {
    println!("{}", sum_pair((4, 5)));
}

You can use _ to ignore unneeded parameters:

#![allow(unused)]
fn main() {
fn do_nothing(_: i32) {
    // This parameter is intentionally unused
}
}

Closures follow the same rules, allowing patterns in their parameter lists.