子切片模式

版本新特性 / Rust 1.42

// *** before: ***
// the middle of a slice could not be named in a pattern
let parts: Vec<&str> = line.split(' ').collect();
if parts.len() >= 2 {
    let first = parts[0];
    let last = parts[parts.len() - 1];
    println!("{} {}", first, last);
}

// *** in version 1.42: ***
match parts.as_slice() {
    [only] => println!("one: {}", only),
    [first, .., last] => println!("{} {}", first, last),
    [] => println!("empty"),
}

// the middle can be bound as well
if let [head, tail @ ..] = parts.as_slice() {
    println!("{} and {} more", head, tail.len());
}