fn main() {
use std::collections::HashMap;
let mut scores = HashMap::new();
scores.insert(String::from("Blue"), 10);
scores.insert(String::from("Yellow"), 50);
let team_name = String::from("Blue");
let score = scores.get(&team_name).copied().unwrap_or(0);
}
Here , im trying to get a value out of hashmap and, I know its converting the reference value into a owned value by using the copying, but what the use of that, cant i use the reference itself. Got this code from hashmap section of rust book.
Yes, you said it. But again whats the use case of copied() or cloned() function for Option enum. Its actually converting the Option<&T> into an Option< T > . Why here in this example they are doing this?
Here, the combination foo.copied().unwrap_or(0) is just a way to save the effort of writing
match foo {
Some(reference) => *reference,
None => 0,
}
Why dereference (and thus copy) in the Some branch? The types must match! reference is a &i32, whereas 0 is a i32.
Why not make the 0 a reference instead? In fact in this case it does work
match foo {
Some(reference) => reference,
None => &0,
}
or
foo.unwrap_or(&9)
However, this relies on static promotion of the &0 to be usable, and the additional reference indirection sort-of feels “unnecessary” anyways. The static promotion would break down, as soon as the fallback value would be runtime-generated, too:
let score = scores.get(&team_name).copied().unwrap_or(&generate_fallback());
use(score);
^^^ this would result in lifetime errors. These in turn could be avoided by storing the generated fallback in some local variable, but at that time it becomes tedious. And as soon as you do this in a context where you may want to return the i32 (or &i32) from the containing function, using references may no longer work at all, if the fallback value was generated inside the function, and thus owned by it. (If you own it inside of the function, you cannot return a reference to it.) I’m skipping the effort of proactively writing code examples for everything I’ve said in this paragraph, but feel free to ask for some as a follow-up, for any point you cannot imagine yourself based on my description alone.