array::IntoIter 按值迭代

版本新特性 / Rust 1.51

let arr = [String::from("a"), String::from("b")];

// *** 以前 ***
// 数组过去只给出引用,需要值时必须克隆
for s in arr.iter() {
    let owned = s.clone();
    println!("{}", owned);
}

// *** 在 1.51 版本中 ***
for s in std::array::IntoIter::new(arr) {
    let owned: String = s;   // 直接取得值,无需克隆
    println!("{}", owned);
}