std-method

Iterator::copied

Convert shared references to copied values. Choose it for Copy types; it does not duplicate a non-Copy String. Expected stdout (not executed): [4, 9] Common mistake: Using copied with String elements.

Formal Syntax & Structure

rust syntaxrust
fn copied<'a, T: Copy + 'a>(self) -> Copied<Self> where Self: Sized + Iterator<Item = &'a T>

Working Example

Example Snippetrust
fn main() {    let stock = [4, 9];    let owned: Vec<i32> = stock.iter().copied().collect();    println!("{owned:?}");}
Rust Reference