HashVec

Struct HashVec 

Source
pub struct HashVec<K: Eq + Hash, V> { /* private fields */ }

Implementations§

Source§

impl<K: Eq + Hash, V> HashVec<K, V>

Source

pub fn new() -> HashVec<K, V>

Creates a new, empty map.

Source

pub fn with_capacity(capacity: usize) -> HashVec<K, V>

Creates a new, empty hashvec with the specified capacity.

Source

pub fn from_vec(v: Vec<(K, V)>) -> HashVec<K, V>

Creates a hashvec from a vector of key-value pairs.

Internally, this uses HashVec::append_vec(), which means that redundant keys’ entries will be overwritten and moved to the end of the hashvec sequentially.

Source

pub fn capacity(&self) -> usize

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

Source

pub fn len(&self) -> usize

Returns the number of elements in the hashvec.

Examples found in repository?
examples/animals.rs (line 40)
1fn main() {
2    use hashvec::*;
3
4    // Create a new hashvec containing pairs of animal names and species
5    // The hashvec! macro acts like vec!, but with key-value tuple pairs
6    let mut hashvec: HashVec<&'static str, &'static str> = hashvec![
7        ("Doug", "Kobold"),
8        ("Skye", "Hyena"),
9        ("Lee", "Shiba"),
10        ("Sock", "Man"),
11        ("Salad", "Wolf"),
12        ("Finn", "Human")
13    ];
14
15    // Insert a value into the hashvec (HashMap-style)
16    // Inserting overwrites existing keys' entries in-place
17    hashvec.insert("Jake", "Dog");
18
19    // Push a value onto the hashvec (Vector-style)
20    // Pushing overwrites existing keys' entries and moves them to the end
21    hashvec.push(("Susie", "Squid"));
22
23    // Access a value by key
24    match hashvec.get(&"Finn") {
25        Some(value) => {
26            assert_eq!(*value, "Human");
27        },
28        None => {}
29    }
30    
31    // Access an entry by index
32    let lee_value = hashvec[2];
33    assert_eq!(lee_value, ("Lee", "Shiba"));
34    
35    // Get the index of a key
36    let lee_index = hashvec.index(&"Lee").unwrap();
37    assert_eq!(lee_index, 2);
38
39    // Get the length of the hashvec
40    let hashvec_length = hashvec.len();
41    assert_eq!(hashvec_length, 8);
42    
43    // Change an entry's key in-place
44    hashvec.rename(&"Salad", "Caesar");
45    assert_eq!(hashvec[4], ("Caesar", "Wolf"));
46
47    // Mutate a value
48    match hashvec.get_mut(&"Sock") {
49        Some(value) => {
50            *value = "Guinea Pig";
51        },
52        None => {}
53    }
54    assert_eq!(*hashvec.get(&"Sock").unwrap(), "Guinea Pig");
55
56    // Remove an entry
57    hashvec.remove_key(&"Doug");
58    assert_eq!(hashvec.get(&"Doug"), None);
59
60    // Swap the locations of two entries by their keys
61    hashvec.swap_keys(&"Lee", &"Skye");
62    assert_eq!(hashvec.index(&"Lee").unwrap(), 0);
63    assert_eq!(hashvec.index(&"Skye").unwrap(), 1);
64
65    // Now swap them again, by their indices
66    hashvec.swap_indices(0, 1);
67    assert_eq!(hashvec[0], ("Skye", "Hyena"));
68    assert_eq!(hashvec[1], ("Lee", "Shiba"));
69    
70    // Iterate over each of the key-value pairs in the hashvec
71    for (k, v) in hashvec.into_iter() {
72        println!("{} is a {}!", k, v);
73    }
74
75    // Remove an entry from the end of the hashvec
76    let last_entry = hashvec.pop();
77    assert_eq!(last_entry.unwrap(), ("Susie", "Squid"));
78
79    // Clear the hashvec
80    hashvec.clear();
81}
Source

pub fn is_empty(&self) -> bool

Returns true if the hashvec contains no elements.

Source

pub fn clear(&mut self)

Clears the hashvec, removing all entries.

Keep in mind this will not reallocate memory.

Examples found in repository?
examples/animals.rs (line 80)
1fn main() {
2    use hashvec::*;
3
4    // Create a new hashvec containing pairs of animal names and species
5    // The hashvec! macro acts like vec!, but with key-value tuple pairs
6    let mut hashvec: HashVec<&'static str, &'static str> = hashvec![
7        ("Doug", "Kobold"),
8        ("Skye", "Hyena"),
9        ("Lee", "Shiba"),
10        ("Sock", "Man"),
11        ("Salad", "Wolf"),
12        ("Finn", "Human")
13    ];
14
15    // Insert a value into the hashvec (HashMap-style)
16    // Inserting overwrites existing keys' entries in-place
17    hashvec.insert("Jake", "Dog");
18
19    // Push a value onto the hashvec (Vector-style)
20    // Pushing overwrites existing keys' entries and moves them to the end
21    hashvec.push(("Susie", "Squid"));
22
23    // Access a value by key
24    match hashvec.get(&"Finn") {
25        Some(value) => {
26            assert_eq!(*value, "Human");
27        },
28        None => {}
29    }
30    
31    // Access an entry by index
32    let lee_value = hashvec[2];
33    assert_eq!(lee_value, ("Lee", "Shiba"));
34    
35    // Get the index of a key
36    let lee_index = hashvec.index(&"Lee").unwrap();
37    assert_eq!(lee_index, 2);
38
39    // Get the length of the hashvec
40    let hashvec_length = hashvec.len();
41    assert_eq!(hashvec_length, 8);
42    
43    // Change an entry's key in-place
44    hashvec.rename(&"Salad", "Caesar");
45    assert_eq!(hashvec[4], ("Caesar", "Wolf"));
46
47    // Mutate a value
48    match hashvec.get_mut(&"Sock") {
49        Some(value) => {
50            *value = "Guinea Pig";
51        },
52        None => {}
53    }
54    assert_eq!(*hashvec.get(&"Sock").unwrap(), "Guinea Pig");
55
56    // Remove an entry
57    hashvec.remove_key(&"Doug");
58    assert_eq!(hashvec.get(&"Doug"), None);
59
60    // Swap the locations of two entries by their keys
61    hashvec.swap_keys(&"Lee", &"Skye");
62    assert_eq!(hashvec.index(&"Lee").unwrap(), 0);
63    assert_eq!(hashvec.index(&"Skye").unwrap(), 1);
64
65    // Now swap them again, by their indices
66    hashvec.swap_indices(0, 1);
67    assert_eq!(hashvec[0], ("Skye", "Hyena"));
68    assert_eq!(hashvec[1], ("Lee", "Shiba"));
69    
70    // Iterate over each of the key-value pairs in the hashvec
71    for (k, v) in hashvec.into_iter() {
72        println!("{} is a {}!", k, v);
73    }
74
75    // Remove an entry from the end of the hashvec
76    let last_entry = hashvec.pop();
77    assert_eq!(last_entry.unwrap(), ("Susie", "Squid"));
78
79    // Clear the hashvec
80    hashvec.clear();
81}
Source

pub fn insert(&mut self, k: K, v: V)

Inserts an entry into the hashvec, or replaces an existing one.

Examples found in repository?
examples/animals.rs (line 17)
1fn main() {
2    use hashvec::*;
3
4    // Create a new hashvec containing pairs of animal names and species
5    // The hashvec! macro acts like vec!, but with key-value tuple pairs
6    let mut hashvec: HashVec<&'static str, &'static str> = hashvec![
7        ("Doug", "Kobold"),
8        ("Skye", "Hyena"),
9        ("Lee", "Shiba"),
10        ("Sock", "Man"),
11        ("Salad", "Wolf"),
12        ("Finn", "Human")
13    ];
14
15    // Insert a value into the hashvec (HashMap-style)
16    // Inserting overwrites existing keys' entries in-place
17    hashvec.insert("Jake", "Dog");
18
19    // Push a value onto the hashvec (Vector-style)
20    // Pushing overwrites existing keys' entries and moves them to the end
21    hashvec.push(("Susie", "Squid"));
22
23    // Access a value by key
24    match hashvec.get(&"Finn") {
25        Some(value) => {
26            assert_eq!(*value, "Human");
27        },
28        None => {}
29    }
30    
31    // Access an entry by index
32    let lee_value = hashvec[2];
33    assert_eq!(lee_value, ("Lee", "Shiba"));
34    
35    // Get the index of a key
36    let lee_index = hashvec.index(&"Lee").unwrap();
37    assert_eq!(lee_index, 2);
38
39    // Get the length of the hashvec
40    let hashvec_length = hashvec.len();
41    assert_eq!(hashvec_length, 8);
42    
43    // Change an entry's key in-place
44    hashvec.rename(&"Salad", "Caesar");
45    assert_eq!(hashvec[4], ("Caesar", "Wolf"));
46
47    // Mutate a value
48    match hashvec.get_mut(&"Sock") {
49        Some(value) => {
50            *value = "Guinea Pig";
51        },
52        None => {}
53    }
54    assert_eq!(*hashvec.get(&"Sock").unwrap(), "Guinea Pig");
55
56    // Remove an entry
57    hashvec.remove_key(&"Doug");
58    assert_eq!(hashvec.get(&"Doug"), None);
59
60    // Swap the locations of two entries by their keys
61    hashvec.swap_keys(&"Lee", &"Skye");
62    assert_eq!(hashvec.index(&"Lee").unwrap(), 0);
63    assert_eq!(hashvec.index(&"Skye").unwrap(), 1);
64
65    // Now swap them again, by their indices
66    hashvec.swap_indices(0, 1);
67    assert_eq!(hashvec[0], ("Skye", "Hyena"));
68    assert_eq!(hashvec[1], ("Lee", "Shiba"));
69    
70    // Iterate over each of the key-value pairs in the hashvec
71    for (k, v) in hashvec.into_iter() {
72        println!("{} is a {}!", k, v);
73    }
74
75    // Remove an entry from the end of the hashvec
76    let last_entry = hashvec.pop();
77    assert_eq!(last_entry.unwrap(), ("Susie", "Squid"));
78
79    // Clear the hashvec
80    hashvec.clear();
81}
Source

pub fn push(&mut self, entry: (K, V))

Appends an entry to the back of the hashvec.

If an entry with an identical key was already in the hashvec, it is removed before the new entry is inserted.

§Panics

Panics if the new capacity either overflows usize or exceeds isize::MAX bytes.

Examples found in repository?
examples/animals.rs (line 21)
1fn main() {
2    use hashvec::*;
3
4    // Create a new hashvec containing pairs of animal names and species
5    // The hashvec! macro acts like vec!, but with key-value tuple pairs
6    let mut hashvec: HashVec<&'static str, &'static str> = hashvec![
7        ("Doug", "Kobold"),
8        ("Skye", "Hyena"),
9        ("Lee", "Shiba"),
10        ("Sock", "Man"),
11        ("Salad", "Wolf"),
12        ("Finn", "Human")
13    ];
14
15    // Insert a value into the hashvec (HashMap-style)
16    // Inserting overwrites existing keys' entries in-place
17    hashvec.insert("Jake", "Dog");
18
19    // Push a value onto the hashvec (Vector-style)
20    // Pushing overwrites existing keys' entries and moves them to the end
21    hashvec.push(("Susie", "Squid"));
22
23    // Access a value by key
24    match hashvec.get(&"Finn") {
25        Some(value) => {
26            assert_eq!(*value, "Human");
27        },
28        None => {}
29    }
30    
31    // Access an entry by index
32    let lee_value = hashvec[2];
33    assert_eq!(lee_value, ("Lee", "Shiba"));
34    
35    // Get the index of a key
36    let lee_index = hashvec.index(&"Lee").unwrap();
37    assert_eq!(lee_index, 2);
38
39    // Get the length of the hashvec
40    let hashvec_length = hashvec.len();
41    assert_eq!(hashvec_length, 8);
42    
43    // Change an entry's key in-place
44    hashvec.rename(&"Salad", "Caesar");
45    assert_eq!(hashvec[4], ("Caesar", "Wolf"));
46
47    // Mutate a value
48    match hashvec.get_mut(&"Sock") {
49        Some(value) => {
50            *value = "Guinea Pig";
51        },
52        None => {}
53    }
54    assert_eq!(*hashvec.get(&"Sock").unwrap(), "Guinea Pig");
55
56    // Remove an entry
57    hashvec.remove_key(&"Doug");
58    assert_eq!(hashvec.get(&"Doug"), None);
59
60    // Swap the locations of two entries by their keys
61    hashvec.swap_keys(&"Lee", &"Skye");
62    assert_eq!(hashvec.index(&"Lee").unwrap(), 0);
63    assert_eq!(hashvec.index(&"Skye").unwrap(), 1);
64
65    // Now swap them again, by their indices
66    hashvec.swap_indices(0, 1);
67    assert_eq!(hashvec[0], ("Skye", "Hyena"));
68    assert_eq!(hashvec[1], ("Lee", "Shiba"));
69    
70    // Iterate over each of the key-value pairs in the hashvec
71    for (k, v) in hashvec.into_iter() {
72        println!("{} is a {}!", k, v);
73    }
74
75    // Remove an entry from the end of the hashvec
76    let last_entry = hashvec.pop();
77    assert_eq!(last_entry.unwrap(), ("Susie", "Squid"));
78
79    // Clear the hashvec
80    hashvec.clear();
81}
Source

pub fn pop(&mut self) -> Option<(K, V)>

Removes the last entry from the hashvec and returns it (or None if the hashvec is empty).

Examples found in repository?
examples/animals.rs (line 76)
1fn main() {
2    use hashvec::*;
3
4    // Create a new hashvec containing pairs of animal names and species
5    // The hashvec! macro acts like vec!, but with key-value tuple pairs
6    let mut hashvec: HashVec<&'static str, &'static str> = hashvec![
7        ("Doug", "Kobold"),
8        ("Skye", "Hyena"),
9        ("Lee", "Shiba"),
10        ("Sock", "Man"),
11        ("Salad", "Wolf"),
12        ("Finn", "Human")
13    ];
14
15    // Insert a value into the hashvec (HashMap-style)
16    // Inserting overwrites existing keys' entries in-place
17    hashvec.insert("Jake", "Dog");
18
19    // Push a value onto the hashvec (Vector-style)
20    // Pushing overwrites existing keys' entries and moves them to the end
21    hashvec.push(("Susie", "Squid"));
22
23    // Access a value by key
24    match hashvec.get(&"Finn") {
25        Some(value) => {
26            assert_eq!(*value, "Human");
27        },
28        None => {}
29    }
30    
31    // Access an entry by index
32    let lee_value = hashvec[2];
33    assert_eq!(lee_value, ("Lee", "Shiba"));
34    
35    // Get the index of a key
36    let lee_index = hashvec.index(&"Lee").unwrap();
37    assert_eq!(lee_index, 2);
38
39    // Get the length of the hashvec
40    let hashvec_length = hashvec.len();
41    assert_eq!(hashvec_length, 8);
42    
43    // Change an entry's key in-place
44    hashvec.rename(&"Salad", "Caesar");
45    assert_eq!(hashvec[4], ("Caesar", "Wolf"));
46
47    // Mutate a value
48    match hashvec.get_mut(&"Sock") {
49        Some(value) => {
50            *value = "Guinea Pig";
51        },
52        None => {}
53    }
54    assert_eq!(*hashvec.get(&"Sock").unwrap(), "Guinea Pig");
55
56    // Remove an entry
57    hashvec.remove_key(&"Doug");
58    assert_eq!(hashvec.get(&"Doug"), None);
59
60    // Swap the locations of two entries by their keys
61    hashvec.swap_keys(&"Lee", &"Skye");
62    assert_eq!(hashvec.index(&"Lee").unwrap(), 0);
63    assert_eq!(hashvec.index(&"Skye").unwrap(), 1);
64
65    // Now swap them again, by their indices
66    hashvec.swap_indices(0, 1);
67    assert_eq!(hashvec[0], ("Skye", "Hyena"));
68    assert_eq!(hashvec[1], ("Lee", "Shiba"));
69    
70    // Iterate over each of the key-value pairs in the hashvec
71    for (k, v) in hashvec.into_iter() {
72        println!("{} is a {}!", k, v);
73    }
74
75    // Remove an entry from the end of the hashvec
76    let last_entry = hashvec.pop();
77    assert_eq!(last_entry.unwrap(), ("Susie", "Squid"));
78
79    // Clear the hashvec
80    hashvec.clear();
81}
Source

pub fn append(&mut self, other: &mut HashVec<K, V>)

Appends all entries of other into Self, leaving other empty.

§Panics

Panics if the number of elements in the hashvec either overflows usize or exceeds isize::MAX bytes

Examples found in repository?
examples/append.rs (line 13)
1fn main() {
2    use hashvec::*;
3
4    let mut a: HashVec<&'static str, &'static str> = hashvec![
5        ("Frank", "Dog"),
6        ("Jimmy", "Pig")
7    ];
8
9    let mut b: HashVec<&'static str, &'static str> = hashvec![
10        ("Mack", "Cat")
11    ];
12
13    a.append(&mut b);
14
15    println!("{:#?}", a);
16    println!("{:#?}", b);
17}
Source

pub fn append_vec(&mut self, v: Vec<(K, V)>)

Appends a vector of key-value pairs onto the hashvec.

Internally, this uses HashVec::push(), which means that redundant keys’ entries will be overwritten and moved to the end of the hashvec sequentially.

Source

pub fn swap_keys(&mut self, key_a: &K, key_b: &K)

Swaps the location of the provided keys’ entries

If either one of the keys is not already in the hashvec, this is a no-op.

Examples found in repository?
examples/animals.rs (line 61)
1fn main() {
2    use hashvec::*;
3
4    // Create a new hashvec containing pairs of animal names and species
5    // The hashvec! macro acts like vec!, but with key-value tuple pairs
6    let mut hashvec: HashVec<&'static str, &'static str> = hashvec![
7        ("Doug", "Kobold"),
8        ("Skye", "Hyena"),
9        ("Lee", "Shiba"),
10        ("Sock", "Man"),
11        ("Salad", "Wolf"),
12        ("Finn", "Human")
13    ];
14
15    // Insert a value into the hashvec (HashMap-style)
16    // Inserting overwrites existing keys' entries in-place
17    hashvec.insert("Jake", "Dog");
18
19    // Push a value onto the hashvec (Vector-style)
20    // Pushing overwrites existing keys' entries and moves them to the end
21    hashvec.push(("Susie", "Squid"));
22
23    // Access a value by key
24    match hashvec.get(&"Finn") {
25        Some(value) => {
26            assert_eq!(*value, "Human");
27        },
28        None => {}
29    }
30    
31    // Access an entry by index
32    let lee_value = hashvec[2];
33    assert_eq!(lee_value, ("Lee", "Shiba"));
34    
35    // Get the index of a key
36    let lee_index = hashvec.index(&"Lee").unwrap();
37    assert_eq!(lee_index, 2);
38
39    // Get the length of the hashvec
40    let hashvec_length = hashvec.len();
41    assert_eq!(hashvec_length, 8);
42    
43    // Change an entry's key in-place
44    hashvec.rename(&"Salad", "Caesar");
45    assert_eq!(hashvec[4], ("Caesar", "Wolf"));
46
47    // Mutate a value
48    match hashvec.get_mut(&"Sock") {
49        Some(value) => {
50            *value = "Guinea Pig";
51        },
52        None => {}
53    }
54    assert_eq!(*hashvec.get(&"Sock").unwrap(), "Guinea Pig");
55
56    // Remove an entry
57    hashvec.remove_key(&"Doug");
58    assert_eq!(hashvec.get(&"Doug"), None);
59
60    // Swap the locations of two entries by their keys
61    hashvec.swap_keys(&"Lee", &"Skye");
62    assert_eq!(hashvec.index(&"Lee").unwrap(), 0);
63    assert_eq!(hashvec.index(&"Skye").unwrap(), 1);
64
65    // Now swap them again, by their indices
66    hashvec.swap_indices(0, 1);
67    assert_eq!(hashvec[0], ("Skye", "Hyena"));
68    assert_eq!(hashvec[1], ("Lee", "Shiba"));
69    
70    // Iterate over each of the key-value pairs in the hashvec
71    for (k, v) in hashvec.into_iter() {
72        println!("{} is a {}!", k, v);
73    }
74
75    // Remove an entry from the end of the hashvec
76    let last_entry = hashvec.pop();
77    assert_eq!(last_entry.unwrap(), ("Susie", "Squid"));
78
79    // Clear the hashvec
80    hashvec.clear();
81}
Source

pub fn swap_indices(&mut self, index_a: usize, index_b: usize)

Swaps the location of the entries at the provided indices

If either one of the indices exceeds the current length of the hashvec, this is a no-op.

Examples found in repository?
examples/animals.rs (line 66)
1fn main() {
2    use hashvec::*;
3
4    // Create a new hashvec containing pairs of animal names and species
5    // The hashvec! macro acts like vec!, but with key-value tuple pairs
6    let mut hashvec: HashVec<&'static str, &'static str> = hashvec![
7        ("Doug", "Kobold"),
8        ("Skye", "Hyena"),
9        ("Lee", "Shiba"),
10        ("Sock", "Man"),
11        ("Salad", "Wolf"),
12        ("Finn", "Human")
13    ];
14
15    // Insert a value into the hashvec (HashMap-style)
16    // Inserting overwrites existing keys' entries in-place
17    hashvec.insert("Jake", "Dog");
18
19    // Push a value onto the hashvec (Vector-style)
20    // Pushing overwrites existing keys' entries and moves them to the end
21    hashvec.push(("Susie", "Squid"));
22
23    // Access a value by key
24    match hashvec.get(&"Finn") {
25        Some(value) => {
26            assert_eq!(*value, "Human");
27        },
28        None => {}
29    }
30    
31    // Access an entry by index
32    let lee_value = hashvec[2];
33    assert_eq!(lee_value, ("Lee", "Shiba"));
34    
35    // Get the index of a key
36    let lee_index = hashvec.index(&"Lee").unwrap();
37    assert_eq!(lee_index, 2);
38
39    // Get the length of the hashvec
40    let hashvec_length = hashvec.len();
41    assert_eq!(hashvec_length, 8);
42    
43    // Change an entry's key in-place
44    hashvec.rename(&"Salad", "Caesar");
45    assert_eq!(hashvec[4], ("Caesar", "Wolf"));
46
47    // Mutate a value
48    match hashvec.get_mut(&"Sock") {
49        Some(value) => {
50            *value = "Guinea Pig";
51        },
52        None => {}
53    }
54    assert_eq!(*hashvec.get(&"Sock").unwrap(), "Guinea Pig");
55
56    // Remove an entry
57    hashvec.remove_key(&"Doug");
58    assert_eq!(hashvec.get(&"Doug"), None);
59
60    // Swap the locations of two entries by their keys
61    hashvec.swap_keys(&"Lee", &"Skye");
62    assert_eq!(hashvec.index(&"Lee").unwrap(), 0);
63    assert_eq!(hashvec.index(&"Skye").unwrap(), 1);
64
65    // Now swap them again, by their indices
66    hashvec.swap_indices(0, 1);
67    assert_eq!(hashvec[0], ("Skye", "Hyena"));
68    assert_eq!(hashvec[1], ("Lee", "Shiba"));
69    
70    // Iterate over each of the key-value pairs in the hashvec
71    for (k, v) in hashvec.into_iter() {
72        println!("{} is a {}!", k, v);
73    }
74
75    // Remove an entry from the end of the hashvec
76    let last_entry = hashvec.pop();
77    assert_eq!(last_entry.unwrap(), ("Susie", "Squid"));
78
79    // Clear the hashvec
80    hashvec.clear();
81}
Source

pub fn contains_key(&self, k: &K) -> bool

Returns true if the hashvec contains an entry corresponding to the provided key.

Source

pub fn get(&self, k: &K) -> Option<&V>

Returns a reference to the value corresponding to the key, if it exists.

Examples found in repository?
examples/animals.rs (line 24)
1fn main() {
2    use hashvec::*;
3
4    // Create a new hashvec containing pairs of animal names and species
5    // The hashvec! macro acts like vec!, but with key-value tuple pairs
6    let mut hashvec: HashVec<&'static str, &'static str> = hashvec![
7        ("Doug", "Kobold"),
8        ("Skye", "Hyena"),
9        ("Lee", "Shiba"),
10        ("Sock", "Man"),
11        ("Salad", "Wolf"),
12        ("Finn", "Human")
13    ];
14
15    // Insert a value into the hashvec (HashMap-style)
16    // Inserting overwrites existing keys' entries in-place
17    hashvec.insert("Jake", "Dog");
18
19    // Push a value onto the hashvec (Vector-style)
20    // Pushing overwrites existing keys' entries and moves them to the end
21    hashvec.push(("Susie", "Squid"));
22
23    // Access a value by key
24    match hashvec.get(&"Finn") {
25        Some(value) => {
26            assert_eq!(*value, "Human");
27        },
28        None => {}
29    }
30    
31    // Access an entry by index
32    let lee_value = hashvec[2];
33    assert_eq!(lee_value, ("Lee", "Shiba"));
34    
35    // Get the index of a key
36    let lee_index = hashvec.index(&"Lee").unwrap();
37    assert_eq!(lee_index, 2);
38
39    // Get the length of the hashvec
40    let hashvec_length = hashvec.len();
41    assert_eq!(hashvec_length, 8);
42    
43    // Change an entry's key in-place
44    hashvec.rename(&"Salad", "Caesar");
45    assert_eq!(hashvec[4], ("Caesar", "Wolf"));
46
47    // Mutate a value
48    match hashvec.get_mut(&"Sock") {
49        Some(value) => {
50            *value = "Guinea Pig";
51        },
52        None => {}
53    }
54    assert_eq!(*hashvec.get(&"Sock").unwrap(), "Guinea Pig");
55
56    // Remove an entry
57    hashvec.remove_key(&"Doug");
58    assert_eq!(hashvec.get(&"Doug"), None);
59
60    // Swap the locations of two entries by their keys
61    hashvec.swap_keys(&"Lee", &"Skye");
62    assert_eq!(hashvec.index(&"Lee").unwrap(), 0);
63    assert_eq!(hashvec.index(&"Skye").unwrap(), 1);
64
65    // Now swap them again, by their indices
66    hashvec.swap_indices(0, 1);
67    assert_eq!(hashvec[0], ("Skye", "Hyena"));
68    assert_eq!(hashvec[1], ("Lee", "Shiba"));
69    
70    // Iterate over each of the key-value pairs in the hashvec
71    for (k, v) in hashvec.into_iter() {
72        println!("{} is a {}!", k, v);
73    }
74
75    // Remove an entry from the end of the hashvec
76    let last_entry = hashvec.pop();
77    assert_eq!(last_entry.unwrap(), ("Susie", "Squid"));
78
79    // Clear the hashvec
80    hashvec.clear();
81}
Source

pub fn get_mut(&mut self, k: &K) -> Option<&mut V>

Returns a mutable reference to the value corresponding to the key, if it exists.

Examples found in repository?
examples/animals.rs (line 48)
1fn main() {
2    use hashvec::*;
3
4    // Create a new hashvec containing pairs of animal names and species
5    // The hashvec! macro acts like vec!, but with key-value tuple pairs
6    let mut hashvec: HashVec<&'static str, &'static str> = hashvec![
7        ("Doug", "Kobold"),
8        ("Skye", "Hyena"),
9        ("Lee", "Shiba"),
10        ("Sock", "Man"),
11        ("Salad", "Wolf"),
12        ("Finn", "Human")
13    ];
14
15    // Insert a value into the hashvec (HashMap-style)
16    // Inserting overwrites existing keys' entries in-place
17    hashvec.insert("Jake", "Dog");
18
19    // Push a value onto the hashvec (Vector-style)
20    // Pushing overwrites existing keys' entries and moves them to the end
21    hashvec.push(("Susie", "Squid"));
22
23    // Access a value by key
24    match hashvec.get(&"Finn") {
25        Some(value) => {
26            assert_eq!(*value, "Human");
27        },
28        None => {}
29    }
30    
31    // Access an entry by index
32    let lee_value = hashvec[2];
33    assert_eq!(lee_value, ("Lee", "Shiba"));
34    
35    // Get the index of a key
36    let lee_index = hashvec.index(&"Lee").unwrap();
37    assert_eq!(lee_index, 2);
38
39    // Get the length of the hashvec
40    let hashvec_length = hashvec.len();
41    assert_eq!(hashvec_length, 8);
42    
43    // Change an entry's key in-place
44    hashvec.rename(&"Salad", "Caesar");
45    assert_eq!(hashvec[4], ("Caesar", "Wolf"));
46
47    // Mutate a value
48    match hashvec.get_mut(&"Sock") {
49        Some(value) => {
50            *value = "Guinea Pig";
51        },
52        None => {}
53    }
54    assert_eq!(*hashvec.get(&"Sock").unwrap(), "Guinea Pig");
55
56    // Remove an entry
57    hashvec.remove_key(&"Doug");
58    assert_eq!(hashvec.get(&"Doug"), None);
59
60    // Swap the locations of two entries by their keys
61    hashvec.swap_keys(&"Lee", &"Skye");
62    assert_eq!(hashvec.index(&"Lee").unwrap(), 0);
63    assert_eq!(hashvec.index(&"Skye").unwrap(), 1);
64
65    // Now swap them again, by their indices
66    hashvec.swap_indices(0, 1);
67    assert_eq!(hashvec[0], ("Skye", "Hyena"));
68    assert_eq!(hashvec[1], ("Lee", "Shiba"));
69    
70    // Iterate over each of the key-value pairs in the hashvec
71    for (k, v) in hashvec.into_iter() {
72        println!("{} is a {}!", k, v);
73    }
74
75    // Remove an entry from the end of the hashvec
76    let last_entry = hashvec.pop();
77    assert_eq!(last_entry.unwrap(), ("Susie", "Squid"));
78
79    // Clear the hashvec
80    hashvec.clear();
81}
Source

pub fn rename(&mut self, old_key: &K, new_key: K) -> Option<&V>

Changes an entry’s key, preserving and returning a reference to the associated value.

If the hashvec did not have an entry corresponding to the old key, None is returned.

Examples found in repository?
examples/animals.rs (line 44)
1fn main() {
2    use hashvec::*;
3
4    // Create a new hashvec containing pairs of animal names and species
5    // The hashvec! macro acts like vec!, but with key-value tuple pairs
6    let mut hashvec: HashVec<&'static str, &'static str> = hashvec![
7        ("Doug", "Kobold"),
8        ("Skye", "Hyena"),
9        ("Lee", "Shiba"),
10        ("Sock", "Man"),
11        ("Salad", "Wolf"),
12        ("Finn", "Human")
13    ];
14
15    // Insert a value into the hashvec (HashMap-style)
16    // Inserting overwrites existing keys' entries in-place
17    hashvec.insert("Jake", "Dog");
18
19    // Push a value onto the hashvec (Vector-style)
20    // Pushing overwrites existing keys' entries and moves them to the end
21    hashvec.push(("Susie", "Squid"));
22
23    // Access a value by key
24    match hashvec.get(&"Finn") {
25        Some(value) => {
26            assert_eq!(*value, "Human");
27        },
28        None => {}
29    }
30    
31    // Access an entry by index
32    let lee_value = hashvec[2];
33    assert_eq!(lee_value, ("Lee", "Shiba"));
34    
35    // Get the index of a key
36    let lee_index = hashvec.index(&"Lee").unwrap();
37    assert_eq!(lee_index, 2);
38
39    // Get the length of the hashvec
40    let hashvec_length = hashvec.len();
41    assert_eq!(hashvec_length, 8);
42    
43    // Change an entry's key in-place
44    hashvec.rename(&"Salad", "Caesar");
45    assert_eq!(hashvec[4], ("Caesar", "Wolf"));
46
47    // Mutate a value
48    match hashvec.get_mut(&"Sock") {
49        Some(value) => {
50            *value = "Guinea Pig";
51        },
52        None => {}
53    }
54    assert_eq!(*hashvec.get(&"Sock").unwrap(), "Guinea Pig");
55
56    // Remove an entry
57    hashvec.remove_key(&"Doug");
58    assert_eq!(hashvec.get(&"Doug"), None);
59
60    // Swap the locations of two entries by their keys
61    hashvec.swap_keys(&"Lee", &"Skye");
62    assert_eq!(hashvec.index(&"Lee").unwrap(), 0);
63    assert_eq!(hashvec.index(&"Skye").unwrap(), 1);
64
65    // Now swap them again, by their indices
66    hashvec.swap_indices(0, 1);
67    assert_eq!(hashvec[0], ("Skye", "Hyena"));
68    assert_eq!(hashvec[1], ("Lee", "Shiba"));
69    
70    // Iterate over each of the key-value pairs in the hashvec
71    for (k, v) in hashvec.into_iter() {
72        println!("{} is a {}!", k, v);
73    }
74
75    // Remove an entry from the end of the hashvec
76    let last_entry = hashvec.pop();
77    assert_eq!(last_entry.unwrap(), ("Susie", "Squid"));
78
79    // Clear the hashvec
80    hashvec.clear();
81}
Source

pub fn remove_key_entry(&mut self, k: &K) -> Option<(K, V)>

Removes a key from the hashvec, returning the stored key and value if the key was previously in the hashvec.

Source

pub fn index(&self, k: &K) -> Option<usize>

Returns the index of the provided key, if the key exists.

Examples found in repository?
examples/animals.rs (line 36)
1fn main() {
2    use hashvec::*;
3
4    // Create a new hashvec containing pairs of animal names and species
5    // The hashvec! macro acts like vec!, but with key-value tuple pairs
6    let mut hashvec: HashVec<&'static str, &'static str> = hashvec![
7        ("Doug", "Kobold"),
8        ("Skye", "Hyena"),
9        ("Lee", "Shiba"),
10        ("Sock", "Man"),
11        ("Salad", "Wolf"),
12        ("Finn", "Human")
13    ];
14
15    // Insert a value into the hashvec (HashMap-style)
16    // Inserting overwrites existing keys' entries in-place
17    hashvec.insert("Jake", "Dog");
18
19    // Push a value onto the hashvec (Vector-style)
20    // Pushing overwrites existing keys' entries and moves them to the end
21    hashvec.push(("Susie", "Squid"));
22
23    // Access a value by key
24    match hashvec.get(&"Finn") {
25        Some(value) => {
26            assert_eq!(*value, "Human");
27        },
28        None => {}
29    }
30    
31    // Access an entry by index
32    let lee_value = hashvec[2];
33    assert_eq!(lee_value, ("Lee", "Shiba"));
34    
35    // Get the index of a key
36    let lee_index = hashvec.index(&"Lee").unwrap();
37    assert_eq!(lee_index, 2);
38
39    // Get the length of the hashvec
40    let hashvec_length = hashvec.len();
41    assert_eq!(hashvec_length, 8);
42    
43    // Change an entry's key in-place
44    hashvec.rename(&"Salad", "Caesar");
45    assert_eq!(hashvec[4], ("Caesar", "Wolf"));
46
47    // Mutate a value
48    match hashvec.get_mut(&"Sock") {
49        Some(value) => {
50            *value = "Guinea Pig";
51        },
52        None => {}
53    }
54    assert_eq!(*hashvec.get(&"Sock").unwrap(), "Guinea Pig");
55
56    // Remove an entry
57    hashvec.remove_key(&"Doug");
58    assert_eq!(hashvec.get(&"Doug"), None);
59
60    // Swap the locations of two entries by their keys
61    hashvec.swap_keys(&"Lee", &"Skye");
62    assert_eq!(hashvec.index(&"Lee").unwrap(), 0);
63    assert_eq!(hashvec.index(&"Skye").unwrap(), 1);
64
65    // Now swap them again, by their indices
66    hashvec.swap_indices(0, 1);
67    assert_eq!(hashvec[0], ("Skye", "Hyena"));
68    assert_eq!(hashvec[1], ("Lee", "Shiba"));
69    
70    // Iterate over each of the key-value pairs in the hashvec
71    for (k, v) in hashvec.into_iter() {
72        println!("{} is a {}!", k, v);
73    }
74
75    // Remove an entry from the end of the hashvec
76    let last_entry = hashvec.pop();
77    assert_eq!(last_entry.unwrap(), ("Susie", "Squid"));
78
79    // Clear the hashvec
80    hashvec.clear();
81}
Source

pub fn remove_key(&mut self, k: &K) -> Option<V>

Removes a key from the hashvec, returning the stored value if the key was previously in the hashvec.

Examples found in repository?
examples/animals.rs (line 57)
1fn main() {
2    use hashvec::*;
3
4    // Create a new hashvec containing pairs of animal names and species
5    // The hashvec! macro acts like vec!, but with key-value tuple pairs
6    let mut hashvec: HashVec<&'static str, &'static str> = hashvec![
7        ("Doug", "Kobold"),
8        ("Skye", "Hyena"),
9        ("Lee", "Shiba"),
10        ("Sock", "Man"),
11        ("Salad", "Wolf"),
12        ("Finn", "Human")
13    ];
14
15    // Insert a value into the hashvec (HashMap-style)
16    // Inserting overwrites existing keys' entries in-place
17    hashvec.insert("Jake", "Dog");
18
19    // Push a value onto the hashvec (Vector-style)
20    // Pushing overwrites existing keys' entries and moves them to the end
21    hashvec.push(("Susie", "Squid"));
22
23    // Access a value by key
24    match hashvec.get(&"Finn") {
25        Some(value) => {
26            assert_eq!(*value, "Human");
27        },
28        None => {}
29    }
30    
31    // Access an entry by index
32    let lee_value = hashvec[2];
33    assert_eq!(lee_value, ("Lee", "Shiba"));
34    
35    // Get the index of a key
36    let lee_index = hashvec.index(&"Lee").unwrap();
37    assert_eq!(lee_index, 2);
38
39    // Get the length of the hashvec
40    let hashvec_length = hashvec.len();
41    assert_eq!(hashvec_length, 8);
42    
43    // Change an entry's key in-place
44    hashvec.rename(&"Salad", "Caesar");
45    assert_eq!(hashvec[4], ("Caesar", "Wolf"));
46
47    // Mutate a value
48    match hashvec.get_mut(&"Sock") {
49        Some(value) => {
50            *value = "Guinea Pig";
51        },
52        None => {}
53    }
54    assert_eq!(*hashvec.get(&"Sock").unwrap(), "Guinea Pig");
55
56    // Remove an entry
57    hashvec.remove_key(&"Doug");
58    assert_eq!(hashvec.get(&"Doug"), None);
59
60    // Swap the locations of two entries by their keys
61    hashvec.swap_keys(&"Lee", &"Skye");
62    assert_eq!(hashvec.index(&"Lee").unwrap(), 0);
63    assert_eq!(hashvec.index(&"Skye").unwrap(), 1);
64
65    // Now swap them again, by their indices
66    hashvec.swap_indices(0, 1);
67    assert_eq!(hashvec[0], ("Skye", "Hyena"));
68    assert_eq!(hashvec[1], ("Lee", "Shiba"));
69    
70    // Iterate over each of the key-value pairs in the hashvec
71    for (k, v) in hashvec.into_iter() {
72        println!("{} is a {}!", k, v);
73    }
74
75    // Remove an entry from the end of the hashvec
76    let last_entry = hashvec.pop();
77    assert_eq!(last_entry.unwrap(), ("Susie", "Squid"));
78
79    // Clear the hashvec
80    hashvec.clear();
81}
Source

pub fn reserve(&mut self, additional: usize)

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

§Panics

Panics if the new capacity either overflows usize or exceeds isize::MAX bytes.

Source

pub fn shrink_to(&mut self, min_capacity: usize)

Shrinks the capacity of the hashvec with a lower limit.

The capacity will remain at least as large as both the length and the supplied value.

If the current capacity is less than the lower limit, this is a no-op.

Source

pub fn shrink_to_fit(&mut self)

Shrinks the capacity of the hashvec as much as possible, according to internal rules.

Trait Implementations§

Source§

impl<K: Debug + Eq + Hash, V: Debug> Debug for HashVec<K, V>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<K: Eq + Hash, V> Index<usize> for HashVec<K, V>

Source§

type Output = (K, V)

The returned type after indexing.
Source§

fn index(&self, i: usize) -> &(K, V)

Performs the indexing (container[index]) operation. Read more
Source§

impl<'a, K: Eq + Hash, V> IntoIterator for &'a HashVec<K, V>

Source§

type Item = (&'a K, &'a V)

The type of the elements being iterated over.
Source§

type IntoIter = HashVecIter<'a, K, V>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl<K, V> Freeze for HashVec<K, V>

§

impl<K, V> RefUnwindSafe for HashVec<K, V>

§

impl<K, V> Send for HashVec<K, V>
where K: Send, V: Send,

§

impl<K, V> Sync for HashVec<K, V>
where K: Sync, V: Sync,

§

impl<K, V> Unpin for HashVec<K, V>
where K: Unpin, V: Unpin,

§

impl<K, V> UnwindSafe for HashVec<K, V>
where K: UnwindSafe, V: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.