pub struct HashVec<K: Eq + Hash, V> { /* private fields */ }Implementations§
Source§impl<K: Eq + Hash, V> HashVec<K, V>
impl<K: Eq + Hash, V> HashVec<K, V>
Sourcepub fn with_capacity(capacity: usize) -> HashVec<K, V>
pub fn with_capacity(capacity: usize) -> HashVec<K, V>
Creates a new, empty hashvec with the specified capacity.
Sourcepub fn from_vec(v: Vec<(K, V)>) -> HashVec<K, V>
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.
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the number of elements the hashvec can hold without reallocating.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements in the hashvec.
Examples found in repository?
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}Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the hashvec, removing all entries.
Keep in mind this will not reallocate memory.
Examples found in repository?
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}Sourcepub fn insert(&mut self, k: K, v: V)
pub fn insert(&mut self, k: K, v: V)
Inserts an entry into the hashvec, or replaces an existing one.
Examples found in repository?
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}Sourcepub fn push(&mut self, entry: (K, V))
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?
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}Sourcepub fn pop(&mut self) -> Option<(K, V)>
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?
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}Sourcepub fn append(&mut self, other: &mut HashVec<K, V>)
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?
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}Sourcepub fn append_vec(&mut self, v: Vec<(K, V)>)
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.
Sourcepub fn swap_keys(&mut self, key_a: &K, key_b: &K)
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?
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}Sourcepub fn swap_indices(&mut self, index_a: usize, index_b: usize)
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?
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}Sourcepub fn contains_key(&self, k: &K) -> bool
pub fn contains_key(&self, k: &K) -> bool
Returns true if the hashvec contains an entry corresponding to the provided key.
Sourcepub fn get(&self, k: &K) -> Option<&V>
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?
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}Sourcepub fn get_mut(&mut self, k: &K) -> Option<&mut V>
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?
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}Sourcepub fn rename(&mut self, old_key: &K, new_key: K) -> Option<&V>
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?
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}Sourcepub fn remove_key_entry(&mut self, k: &K) -> Option<(K, V)>
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.
Sourcepub fn index(&self, k: &K) -> Option<usize>
pub fn index(&self, k: &K) -> Option<usize>
Returns the index of the provided key, if the key exists.
Examples found in repository?
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}Sourcepub fn remove_key(&mut self, k: &K) -> Option<V>
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?
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}Sourcepub fn reserve(&mut self, additional: usize)
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.
Sourcepub fn shrink_to(&mut self, min_capacity: usize)
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.
Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrinks the capacity of the hashvec as much as possible, according to internal rules.