std-method

Iterator::cloned

Produce a cloned value for each shared reference requested. Prefer filtering first when only some items need cloning; Clone may retain shared underlying data for some types. Expected stdout (not executed): 2 2 Common mistake: Cloning every item before filtering unnecessarily.

Formal Syntax & Structure

rust syntaxrust
fn cloned<'a, T: Clone + 'a>(self) -> Cloned<Self> where Self: Sized + Iterator<Item = &'a T>

Working Example

Example Snippetrust
fn main() {    let names = vec![String::from("elm"), String::from("oak")];    let owned: Vec<String> = names.iter().cloned().collect();    println!("{} {}", names.len(), owned.len());}
Rust Reference