Logo

Programming-Idioms

History of Idiom 8 > diff from v3 to v4

Edit summary for version 4 by :

Version 3

2015-07-31, 18:56:25

Version 4

2015-08-19, 14:08:18

Idiom #8 Initialize a new Map (associative array)

Declare a new map object x, and provide some (key, value) pairs as initial content.

Idiom #8 Initialize a new Map (associative array)

Declare a new map object x, and provide some (key, value) pairs as initial content.

Imports
use std::collections::BTreeMap;
Code
let mut _x = BTreeMap::new();
_x.insert("one", 1);
_x.insert("two", 2);
Comments bubble
Something different than a BTreeMap might be used, depending on the usage.

The function `new` of the type `BTreeMap` returns a usable map.
The map is stored mutable in the variable `x`.

Maps in Rust are generic but the type is determined by the compiler based on the later usage.
A line such as `x.insert(1,"one")` would therefore not compile.