Struct linear_map::LinearMap
[−]
[src]
pub struct LinearMap<K, V> { // some fields omitted }
A very simple map implementation backed by a vector.
Use it like any map, as long as the number of elements that it stores is very small.
Example (like std's HashMap)
use linear_map::LinearMap; // type inference lets us omit an explicit type signature (which // would be `LinearMap<&str, &str>` in this example). let mut book_reviews = LinearMap::new(); // review some books. book_reviews.insert("Adventures of Huckleberry Finn", "My favorite book."); book_reviews.insert("Grimms' Fairy Tales", "Masterpiece."); book_reviews.insert("Pride and Prejudice", "Very enjoyable."); book_reviews.insert("The Adventures of Sherlock Holmes", "Eye lyked it alot."); // check for a specific one. if !book_reviews.contains_key("Les Misérables") { println!("We've got {} reviews, but Les Misérables ain't one.", book_reviews.len()); } // oops, this review has a lot of spelling mistakes, let's delete it. book_reviews.remove("The Adventures of Sherlock Holmes"); // look up the values associated with some keys. let to_find = ["Pride and Prejudice", "Alice's Adventure in Wonderland"]; for book in to_find.iter() { match book_reviews.get(book) { Some(review) => println!("{}: {}", *book, *review), None => println!("{} is unreviewed.", *book) } } // iterate over everything. for (book, review) in book_reviews.iter() { println!("{}: \"{}\"", *book, *review); }
Methods
impl<K: Eq, V> LinearMap<K, V>
[src]
fn new() -> LinearMap<K, V>
Creates an empty map. This method does not allocate.
fn with_capacity(capacity: usize) -> LinearMap<K, V>
Creates an empty map with the given initial capacity.
fn capacity(&self) -> usize
Returns the number of elements the map can hold without reallocating.
fn reserve(&mut self, additional: usize)
Reserves capacity for at least additional
more to be inserted in the
map. The collection may reserve more space to avoid frequent
reallocations.
Panics
Panics if the new allocation size overflows usize
.
fn reserve_exact(&mut self, additional: usize)
Reserves the minimum capacity for exactly additional
more elemnnts to
be inserted in the map.
Note that the allocator may give the collection more space than it
requests. Therefore capacity cannot be relied upon to be precisely
minimal. Prefer reserve
if future insertions are expected.
Panics
Panics if the new capacity overflows usize
.
fn shrink_to_fit(&mut self)
Shrinks the capacity of the map as much as possible.
It will drop down as close as possible to the current length but the allocator may still inform the map that there is more space than necessary. Therefore capacity cannot be relid upon to be minimal.
fn len(&self) -> usize
Returns the number of elements in the map.
fn is_empty(&self) -> bool
Returns true if the map contains no elements.
fn clear(&mut self)
Clears the map, removing all elements. Keeps the allocated memory for reuse.
fn iter<'a>(&'a self) -> Iter<'a, K, V>
An iterator visiting all key-value pairs in arbitrary order. Iterator
element type is (&'a K, &'a V)
.
fn iter_mut<'a>(&'a mut self) -> IterMut<'a, K, V>
An iterator visiting all key-value pairs in arbitrary order with
mutable references to the values. Iterator element type is (&'a K, &'a mut V)
.
fn keys<'a>(&'a self) -> Keys<'a, K, V>
An iterator visiting all keys in arbitrary order. Iterator element type
is &'a K
.
fn values<'a>(&'a self) -> Values<'a, K, V>
An iterator visiting all values in arbitrary order. Iterator element
type is &'a V
.
fn get<Q: ?Sized>(&self, key: &Q) -> Option<&V> where K: Borrow<Q>, Q: Eq
Returns a reference to the value corresponding to the key.
fn get_mut<Q: ?Sized>(&mut self, key: &Q) -> Option<&mut V> where K: Borrow<Q>, Q: Eq
Returns a mutable reference to the value corresponding to the key.
fn contains_key<Q: ?Sized>(&self, key: &Q) -> bool where K: Borrow<Q>, Q: Eq
Returns true if the map contains a value to the specified key.
fn insert(&mut self, key: K, value: V) -> Option<V>
Inserts a key-value pair into the map. If the key already had a value
present in the map, it is returned. Otherwise, None
is returned.
fn remove<Q: ?Sized>(&mut self, key: &Q) -> Option<V> where K: Borrow<Q>, Q: Eq
Removes a key-value pair from the map. If the key had a value present
in the map, it is returned. Otherwise, None
is returned.
fn entry(&mut self, key: K) -> Entry<K, V>
Gets the given key's corresponding entry in the map for in-place manipulation.
Trait Implementations
impl<K: Clone, V: Clone> Clone for LinearMap<K, V>
[src]
fn clone(&self) -> LinearMap<K, V>
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0
Performs copy-assignment from source
. Read more
impl<K, V> Debug for LinearMap<K, V> where K: Eq + Debug, V: Debug
[src]
impl<K, V> Default for LinearMap<K, V> where K: Eq
[src]
impl<K, V> Extend<(K, V)> for LinearMap<K, V> where K: Eq
[src]
fn extend<I: IntoIterator<Item=(K, V)>>(&mut self, key_values: I)
Extends a collection with the contents of an iterator. Read more
impl<K, V> FromIterator<(K, V)> for LinearMap<K, V> where K: Eq
[src]
fn from_iter<I: IntoIterator<Item=(K, V)>>(key_values: I) -> Self
Creates a value from an iterator. Read more
impl<K, V> Hash for LinearMap<K, V> where K: Eq + Hash, V: Hash
[src]
fn hash<H: Hasher>(&self, h: &mut H)
Feeds this value into the state given, updating the hasher as necessary.
fn hash_slice<H>(data: &[Self], state: &mut H) where H: Hasher
1.3.0
Feeds a slice of this type into the state provided.
impl<'a, K, V, Q: ?Sized> Index<&'a Q> for LinearMap<K, V> where K: Eq + Borrow<Q>, Q: Eq
[src]
type Output = V
The returned type after indexing
fn index(&self, key: &'a Q) -> &V
The method for the indexing (Foo[Bar]
) operation
impl<K, V> PartialEq for LinearMap<K, V> where K: Eq, V: PartialEq
[src]
fn eq(&self, other: &Self) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, other: &Rhs) -> bool
1.0.0
This method tests for !=
.
impl<K, V> Eq for LinearMap<K, V> where K: Eq, V: Eq
[src]
impl<K, V> PartialOrd for LinearMap<K, V> where K: Eq + PartialOrd, V: PartialOrd
[src]
fn partial_cmp(&self, other: &Self) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
fn lt(&self, other: &Rhs) -> bool
1.0.0
This method tests less than (for self
and other
) and is used by the <
operator. Read more
fn le(&self, other: &Rhs) -> bool
1.0.0
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
fn gt(&self, other: &Rhs) -> bool
1.0.0
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
fn ge(&self, other: &Rhs) -> bool
1.0.0
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more
impl<K, V> Ord for LinearMap<K, V> where K: Ord, V: Ord
[src]
fn cmp(&self, other: &Self) -> Ordering
This method returns an Ordering
between self
and other
. Read more
impl<K, V> IntoIterator for LinearMap<K, V> where K: Eq
[src]
type Item = (K, V)
The type of the elements being iterated over.
type IntoIter = IntoIter<K, V>
Which kind of iterator are we turning this into?
fn into_iter(self) -> IntoIter<K, V>
Creates an iterator from a value. Read more
impl<'a, K, V> IntoIterator for &'a LinearMap<K, V> where K: Eq
[src]
type Item = (&'a K, &'a V)
The type of the elements being iterated over.
type IntoIter = Iter<'a, K, V>
Which kind of iterator are we turning this into?
fn into_iter(self) -> Iter<'a, K, V>
Creates an iterator from a value. Read more