std-method

Option::copied / cloned

Convert an optional reference into an optional owned payload. copied requires Copy; cloned uses the type’s Clone behavior, which can retain shared ownership. Expected stdout (not executed): Some(12) Common mistake: Assuming cloned always creates independent underlying data.

Formal Syntax & Structure

rust syntaxrust
Option<&T>::copied(self) -> Option<T> where T: Copy; Option<&T>::cloned(self) -> Option<T> where T: Clone

Working Example

Example Snippetrust
fn main() {    let n = 12;    println!("{:?}", Some(&n).copied());}
Rust Reference