HashMap.get() dereferenced

What is the idiomatic way to get a dereferenced copy (in case of Copy trait) of a value from a hashmap or other collection that returns a Option<&Value> where Value: Copy?

Do I have to match

match collection.get(key) {
  None => None,
  Some(v) => Some(*v),
}

or is there a nicer way?

The recently stabilized Option::copied

2 Likes

For a non-Copy type, do Option::copied() and Option::cloned() have identical performance?

Option::copied requires T: Copy, because ... well, you can't copy non-copy types.

for Copy types clone and copy are identical.

Not always, for example in generic code you may see differences.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.