Transforming Option<&u8> into Option<u8>

Hi,

I'm new to Rust and I'm wondering what's the idiomatic way to transform an Option of a reference into an Option of a value?

You can use Option::copied for Copy types (like u8), or Option::cloned for Clone.

let x = 123u8;
let y: Option<&u8> = Some(&x);
let z: Option<u8> = y.copied();
3 Likes

You might want to use Option::map().

Thanks!