Reputation: 1497
Why does HashMap.find_or_insert(k,v) return a &mut type, and how do it get it to return a type
?
I just started using Rust for a course, and I am using a HashMap<int, int>
and want to get an int
back.
let mut m: HashMap<int, int> = HashMap::new();
println!("{:d}", m.find_or_insert(1,2));
gives me an error saying that it failed to find an implementation of trait std::fnt::Signed for &mut int
.
Edit1:
I am using Rust 0.9 on Windows 8.1, using msys.
My code so far
use std::hashmap::HashMap;
fn main() {
let mut m: HashMap<int, int> = HashMap::new();
println!("{:d}", *m.find_or_insert(1,2))
}
I tried this code again and it properly returns 2
Upvotes: 2
Views: 2488
Reputation: 225144
Why does find_or_insert
return a reference? Copying isn’t always possible/efficient.
How can you use that reference in the case of an integer? Dereference it with a *
:
let mut m: HashMap<int, int> = HashMap::new();
println!("{:d}", *m.find_or_insert(1,2));
Upvotes: 3