Struct json_api::value::collections::map::Map [] [src]

pub struct Map<K = Key, V = Value> where
    K: Eq + Hash
{ /* fields omitted */ }

A hash map implementation with consistent ordering.

Methods

impl<K, V> Map<K, V> where
    K: Eq + Hash
[src]

[src]

Creates an empty Map.

Example

use json_api::value::{Key, Map, Value};
let mut map = Map::<Key, Value>::new();

[src]

Creates a new empty Map, with specified capacity.

Example

let mut map = Map::with_capacity(2);

map.insert("x", 1);
map.insert("y", 2);

// The next insert will likely require reallocation...
map.insert("z", 3);

[src]

Returns the number of key-value pairs the map can hold without reallocating.

Example

let map = Map::<Key, Value>::with_capacity(2);
assert!(map.capacity() >= 2);

[src]

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

Example

let mut map = Map::new();

map.insert("x", 1);
map.clear();
assert!(map.is_empty());

[src]

Returns true if the map contains a value for the specified key.

Example

let mut map = Map::new();

map.insert(1, "a");
assert_eq!(map.contains_key(&1), true);
assert_eq!(map.contains_key(&2), false);

[src]

Clears the map, returning all key-value pairs as an iterator. Keeps the allocated memory for reuse.

Example

let mut map = Map::new();

map.insert("x", 1);
map.insert("y", 2);

for (key, value) in map.drain(..) {
    assert!(key == "x" || key == "y");
    assert!(value == 1 || value == 2);
}

assert!(map.is_empty());

[src]

Returns a reference to the value corresponding to the key.

Example

let mut map = Map::new();

map.insert("x", 1);

assert_eq!(map.get("x"), Some(&1));
assert_eq!(map.get("y"), None);

[src]

Inserts a key-value pair into the map.

If a value already existed for key, that old value is returned in Some; otherwise, None is returned.

Example

let mut map = Map::new();

assert_eq!(map.insert("x", 1), None);
assert_eq!(map.insert("x", 2), Some(1));

[src]

Return an iterator visiting all the key-value pairs of the map in the order in which they were inserted.

Example

let mut map = Map::new();

map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);

for (key, value) in map.iter() {
    println!("key: {} value: {}", key, value);
}

[src]

Return an iterator visiting all the key-value pairs of the map in the order in which they were inserted, with mutable references to the values.

Example

let mut map = Map::new();

map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);

for (_, value) in map.iter_mut() {
    *value += 1;
}

for (key, value) in &map {
    println!("key: {} value: {}", key, value);
}

[src]

Return an iterator visiting all keys in the order in which they were inserted.

Example

let mut map = Map::new();

map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);

for key in map.keys() {
    println!("{}", key);
}

[src]

Return the number of key-value pairs in the map.

Example

let mut map = Map::new();
assert_eq!(map.len(), 0);

map.insert("x", 1);
assert_eq!(map.len(), 1);

[src]

Returns true if the map contains no elements.

Example

let mut map = Map::new();
assert!(map.is_empty());

map.insert("x", 1);
assert!(!map.is_empty());

[src]

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

Example

let mut map = Map::new();

map.insert("x", 1);

assert_eq!(map.remove("x"), Some(1));
assert_eq!(map.remove("x"), None);

[src]

Reserves capacity for at least additional more elements to be inserted in the Map. The collection may reserve more space to avoid frequent reallocations.

Note

This method has yet to be fully implemented in the ordermap crate.

Example

let mut map = Map::<Key, Value>::new();
map.reserve(10);

[src]

Return an iterator visiting all values in the order in which they were inserted.

Example

let mut map = Map::new();

map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);

for value in map.values() {
    println!("{}", value);
}

[src]

Return an iterator visiting all values mutably in the order in which they were inserted.

Example

let mut map = Map::new();

map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);

for value in map.values_mut() {
    *value += 1;
}

for value in map.values() {
    println!("{}", value);
}

Trait Implementations

impl<K: Clone, V: Clone> Clone for Map<K, V> where
    K: Eq + Hash
[src]

[src]

Returns a copy of the value. Read more

1.0.0
[src]

Performs copy-assignment from source. Read more

impl<K: Eq, V: Eq> Eq for Map<K, V> where
    K: Eq + Hash
[src]

impl<K: PartialEq, V: PartialEq> PartialEq for Map<K, V> where
    K: Eq + Hash
[src]

[src]

This method tests for self and other values to be equal, and is used by ==. Read more

[src]

This method tests for !=.

impl<K, V> Debug for Map<K, V> where
    K: Debug + Eq + Hash,
    V: Debug
[src]

[src]

Formats the value using the given formatter.

impl<K, V> Default for Map<K, V> where
    K: Eq + Hash
[src]

[src]

Returns the "default value" for a type. Read more

impl<K, V> Extend<(K, V)> for Map<K, V> where
    K: Eq + Hash
[src]

[src]

Extends a collection with the contents of an iterator. Read more

impl<K, V> FromIterator<(K, V)> for Map<K, V> where
    K: Eq + Hash
[src]

[src]

Creates a value from an iterator. Read more

impl<K, V> IntoIterator for Map<K, V> where
    K: Eq + Hash
[src]

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

[src]

Creates an iterator from a value. Read more

impl<'a, K, V> IntoIterator for &'a Map<K, V> where
    K: Eq + Hash
[src]

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

[src]

Creates an iterator from a value. Read more

impl<'a, K, V> IntoIterator for &'a mut Map<K, V> where
    K: Eq + Hash
[src]

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

[src]

Creates an iterator from a value. Read more

impl<'de, K, V> Deserialize<'de> for Map<K, V> where
    K: Deserialize<'de> + Eq + Hash,
    V: Deserialize<'de>, 
[src]

[src]

Deserialize this value from the given Serde deserializer. Read more

impl<K, V> Serialize for Map<K, V> where
    K: Eq + Hash + Serialize,
    V: Serialize
[src]

[src]

Serialize this value into the given Serde serializer. Read more