trait

ToOwned

Turn borrowed data into its associated owned type. For a borrowed slice of Clone elements, the owned type is Vec; for str it is String. Expected stdout (not executed): [6, 7] [6, 7, 8] Common mistake: Assuming cloning a slice reference and converting the slice to owned data are equivalent.

Formal Syntax & Structure

rust syntaxrust
value.to_owned()

Working Example

Example Snippetrust
fn main() {    let view: &[i32] = &[6, 7];    let mut owned: Vec<i32> = view.to_owned();    owned.push(8);    println!("{view:?} {owned:?}");}
Rust Reference