HashMap does not work?


#1

Hello all.

I’m trying to dig into Rust world, but stuck into problems at the very beginning. According to the manuals and book and samples, code:

use std::collections::HashMap;
use std::hash::Hash;

fn main() {
    let mut hmap = HashMap::new;
    hmap.insert("hello", "world");
    println!("{}", hmap.get("hello").unwrap());
}

should just work. But it does not even compile:

$ rustc main.rs
main.rs:6:10: 6:34 error: type `fn() -> std::collections::hash::map::HashMap<_, _> {std::collections::hash::map::HashMap<K, V, RandomState>::new}` does not implement any method in scope named `insert`
main.rs:6     hmap.insert("hello", "world");
                   ^~~~~~~~~~~~~~~~~~~~~~~~
main.rs:7:25: 7:37 error: type `fn() -> std::collections::hash::map::HashMap<_, _> {std::collections::hash::map::HashMap<K, V, RandomState>::new}` does not implement any method in scope named `get`
main.rs:7     println!("{}", hmap.get("hello").unwrap());
                                  ^~~~~~~~~~~~
error: aborting due to 2 previous errors

Probably this is because some recent changes into the language, but what is a place to look for them?

Sorry for stupid question, but I’m ever does not know a better place to ask it.


#2

Change this to let mut hmap = HashMap::new().


#3

Ouch! That was stupid.

Thank you very much!


#4

Rust gives informative error messages, so learn to pay attention to them. hmap is a function that yields a HashMap, it’s not a HashMap.