Struct ::std::collections::HashMap

Overview

Methods

fn new() -> HashMap

Creates an empty HashMap.

The hash map is initially created with a capacity of 0, so it will not allocate until it is first inserted into.

Examples

use std::collections::HashMap;
let map = HashMap::new();
fn with_capacity(capacity: i64) -> HashMap

Creates an empty HashMap with at least the specified capacity.

The hash map will be able to hold at least capacity elements without reallocating. This method is allowed to allocate for more elements than capacity. If capacity is 0, the hash map will not allocate.

Examples

use std::collections::HashMap;
let map = HashMap::with_capacity(10);
fn len(self) -> i64

Returns the number of elements in the map.

Examples

use std::collections::HashMap;

let a = HashMap::new();
assert_eq!(a.len(), 0);
a.insert(1, "a");
assert_eq!(a.len(), 1);
fn capacity(self) -> i64

Returns the number of elements the map can hold without reallocating.

This number is a lower bound; the HashMap<K, V> might be able to hold more, but is guaranteed to be able to hold at least this many.

Examples

use std::collections::HashMap;
let map = HashMap::with_capacity(100);
assert!(map.capacity() >= 100);
fn insert(self, key, value) -> Option

Inserts a key-value pair into the map.

If the map did not have this key present, [None] is returned.

If the map did have this key present, the value is updated, and the old value is returned. The key is not updated, though; this matters for types that can be == without being identical. See the module-level documentation for more.

Examples

use std::collections::HashMap;

let map = HashMap::new();
assert_eq!(map.insert(37, "a"), None);
assert_eq!(map.is_empty(), false);

map.insert(37, "b");
assert_eq!(map.insert(37, "c"), Some("b"));
assert_eq!(map[37], "c");
fn get(self, key) -> Option

Returns the value corresponding to the [Key].

Examples

use std::collections::HashMap;

let map = HashMap::new();
map.insert(1, "a");
assert_eq!(map.get(1), Some("a"));
assert_eq!(map.get(2), None);
fn contains_key(self, key) -> bool

Returns true if the map contains a value for the specified [Key].

Examples

use std::collections::HashMap;

let map = HashMap::new();
map.insert(1, "a");
assert_eq!(map.contains_key(1), true);
assert_eq!(map.contains_key(2), false);
fn remove(self, key) -> Option

Removes a key from the map, returning the value at the [Key] if the key was previously in the map.

Examples

use std::collections::HashMap;

let map = HashMap::new();
map.insert(1, "a");
assert_eq!(map.remove(1), Some("a"));
assert_eq!(map.remove(1), None);
fn clear(self) -> Tuple

Clears the map, removing all key-value pairs. Keeps the allocated memory for reuse.

Examples

use std::collections::HashMap;

let a = HashMap::new();
a.insert(1, "a");
a.clear();
assert!(a.is_empty());
fn is_empty(self) -> bool

Returns true if the map contains no elements.

Examples

use std::collections::HashMap;

let a = HashMap::new();
assert!(a.is_empty());
a.insert(1, "a");
assert!(!a.is_empty());
fn iter(self) -> Iterator

An iterator visiting all key-value pairs in arbitrary order.

Examples

use std::collections::HashMap;

let map = HashMap::from([
   ("a", 1),
   ("b", 2),
   ("c", 3),
]);

let pairs = map.iter().collect::<Vec>();
pairs.sort();
assert_eq!(pairs, [("a", 1), ("b", 2), ("c", 3)]);

Performance

In the current implementation, iterating over map takes O(capacity) time instead of O(len) because it internally visits empty buckets too.

fn keys(self) -> Iterator

An iterator visiting all keys in arbitrary order.

Examples

use std::collections::HashMap;

let map = HashMap::from([
   ("a", 1),
   ("b", 2),
   ("c", 3),
]);

let keys = map.keys().collect::<Vec>();
keys.sort();
assert_eq!(keys, ["a", "b", "c"]);

Performance

In the current implementation, iterating over keys takes O(capacity) time instead of O(len) because it internally visits empty buckets too.

An iterator visiting all values in arbitrary order.

Examples

use std::collections::HashMap;

let map = HashMap::from([
   ("a", 1),
   ("b", 2),
   ("c", 3),
]);

let values = map.values().collect::<Vec>();
values.sort();
assert_eq!(values, [1, 2, 3]);

Performance

In the current implementation, iterating over values takes O(capacity) time instead of O(len) because it internally visits empty buckets too.

fn extend(self, value) -> Tuple

Extend this map from an iterator.

Examples

use std::collections::HashMap;

let map = HashMap::new();

map.extend([
   ("a", 1),
   ("b", 2),
   ("c", 3),
]);
fn from(value) -> HashMap

Convert a hashmap from a value.

The hashmap can be converted from anything that implements the [INTO_ITER] protocol, and each item produces should be a tuple pair.

fn clone(self) -> HashMap

Clone the map.

Examples

use std::collections::HashMap;

let a = HashMap::from([("a", 1), ("b", 2)]);
let b = a.clone();

b.insert("c", 3);

assert_eq!(a.len(), 2);
assert_eq!(b.len(), 3);

Protocols

protocol index_set
value[index] = input

Inserts a key-value pair into the map.

If the map did have this key present, the value is updated.

Examples

use std::collections::HashMap;

let map = HashMap::new();
map[37] = "a";
assert!(!map.is_empty());

map[37] = "c";
assert_eq!(map[37], "c");
protocol index_get
let output = value[index]

Returns a the value corresponding to the key.

Panics

Panics if the given value is not present in the map.

use std::collections::HashMap;

let map = HashMap::new();
let _ = map[1];

Examples

use std::collections::HashMap;

let map = HashMap::new();
map[1] = "a";
assert_eq!(map[1], "a");
protocol string_debug
println("{:?}", value)

Debug format the current map.

Examples

use std::collections::HashMap;

let map = HashMap::new();
map[1] = "a";

assert_eq!(format!("{:?}", map), "{1: \"a\"}");
protocol partial_eq
if value == b { }

Perform a partial equality check over two maps.

Examples

use std::collections::HashMap;

let map1 = HashMap::from([
   ("a", 1.0),
   ("c", 3.0),
   ("b", 2.0),
]);

let map2 = HashMap::from([
   ("c", 3.0),
   ("a", 1.0),
   ("b", 2.0),
]);

assert!(map1 == map2);

map1["b"] = f64::NAN;
map2["b"] = f64::NAN;

assert!(map1 != map2);
protocol eq
if value == b { }

Perform a total equality check over two maps.

Examples

use std::collections::HashMap;
use std::ops::eq;

let map1 = HashMap::from([
   ("a", 1),
   ("c", 3),
   ("b", 2),
]);

let map2 = HashMap::from([
   ("c", 3),
   ("a", 1),
   ("b", 2),
]);

assert!(eq(map1, map2));
protocol into_iter
for item in value { }

An iterator visiting all key-value pairs in arbitrary order.

Examples

use std::collections::HashMap;

let map = HashMap::from([
   ("a", 1),
   ("b", 2),
   ("c", 3),
]);

let pairs = [];

for pair in map {
   pairs.push(pair);
}

pairs.sort();
assert_eq!(pairs, [("a", 1), ("b", 2), ("c", 3)]);

Performance

In the current implementation, iterating over map takes O(capacity) time instead of O(len) because it internally visits empty buckets too.