Logo

Programming-Idioms

History of Idiom 8 > diff from v16 to v17

Edit summary for version 17 by :

Version 16

2015-10-29, 13:03:15

Version 17

2015-10-29, 14:05:11

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;
Imports
use std::collections::BTreeMap;
Code
let mut x = BTreeMap::new();
x.insert("one", 1);
x.insert("two", 2);
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.
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.
Demo URL
https://play.rust-lang.org/?code=use%20std%3A%3Acollections%3A%3ABTreeMap%3B%0A%0Afn%20main()%20%7B%0A%20%20%20%20let%20mut%20x%20%3D%20BTreeMap%3A%3Anew()%3B%0A%20%20%20%20x.insert(%22one%22%2C%201)%3B%0A%20%20%20%20x.insert(%22two%22%2C%202)%3B%0A%20%20%20%20%0A%20%20%20%20println!(%22%7B%3A%3F%7D%22%2C%20x)%3B%0A%7D%0A&version=stable
Demo URL
https://play.rust-lang.org/?code=use%20std%3A%3Acollections%3A%3ABTreeMap%3B%0A%0Afn%20main()%20%7B%0A%20%20%20%20let%20mut%20x%20%3D%20BTreeMap%3A%3Anew()%3B%0A%20%20%20%20x.insert(%22one%22%2C%201)%3B%0A%20%20%20%20x.insert(%22two%22%2C%202)%3B%0A%20%20%20%20%0A%20%20%20%20println!(%22%7B%3A%3F%7D%22%2C%20x)%3B%0A%7D%0A&version=stable
Code
$x = array( "one"=>1, "two"=>2 );
Code
$x = array( "one"=>1, "two"=>2 );
Doc URL
http://php.net/manual/en/language.types.array.php
Doc URL
http://php.net/manual/en/language.types.array.php
Code
var x = {
	"one": 1,
	"two": 2
};
Code
var x = {
	"one": 1,
	"two": 2
};
Doc URL
https://www.dartlang.org/docs/dart-up-and-running/contents/ch03.html#ch03-maps-aka-dictionaries-or-hashes
Doc URL
https://www.dartlang.org/docs/dart-up-and-running/contents/ch03.html#ch03-maps-aka-dictionaries-or-hashes
Code
var x = {"one": 1, "two":2}
Code
var x = {"one": 1, "two":2}
Comments bubble
This JSON object is essentially an associative array
Comments bubble
This JSON object is essentially an associative array
Code
x = {"one" => 1, "two" => 2}
Code
x = {"one" => 1, "two" => 2}
Imports
#include <map>
Imports
#include <map>
Code
std::map<const char*, int> x;
x["one"] = 1;
x["two"] = 2;
Code
std::map<const char*, int> x;
x["one"] = 1;
x["two"] = 2;
Doc URL
http://www.cplusplus.com/reference/map/map/
Doc URL
http://www.cplusplus.com/reference/map/map/
Imports
import java.util.Map;
import java.util.HashMap;
Imports
import java.util.Map;
import java.util.HashMap;
Code
Map<String,Integer> x = new HashMap<>();
x.put("one", 1);
x.put("two", 2);
Code
Map<String,Integer> x = new HashMap<>();
x.put("one", 1);
x.put("two", 2);
Demo URL
http://ideone.com/iq8mh4
Demo URL
http://ideone.com/iq8mh4