std-method

slice::to_vec

Create an owned vector by cloning each element of the slice. The vector can grow independently, although a cloned element may itself share resources. Expected stdout (not executed): Ada Ada! Common mistake: Assuming every Clone implementation duplicates all underlying resources.

Formal Syntax & Structure

rust syntaxrust
slice.to_vec()

Working Example

Example Snippetrust
fn main() {    let names = [String::from("Ada"), String::from("Lin")];    let mut owned = names.to_vec();    owned[0].push('!');    println!("{} {}", names[0], owned[0]);}
Rust Reference