[][src]Struct map1::btree_map::BTreeMap1

pub struct BTreeMap1<K, V>(pub BTreeMap<K, V>);

A wrapper around std::collections::BTreeMap that guarantees that it will never be empty.

Methods

impl<K: Ord, V> BTreeMap1<K, V>[src]

pub fn new(key: K, value: V) -> Self[src]

Makes a new BTreeMap1 with a single key-value pair.

Examples

Basic usage:

use map1::BTreeMap1;

let mut map = BTreeMap1::new(2, "b");

// entries can now be inserted into the empty map
map.insert(1, "a");Run

pub fn get<Q: ?Sized>(&self, key: &Q) -> Option<&V> where
    K: Borrow<Q>,
    Q: Ord
[src]

Returns a reference to the value corresponding to the key.

The key may be any borrowed form of the map's key type, but the ordering on the borrowed form must match the ordering on the key type.

Examples

Basic usage:

use map1::BTreeMap1;

let map = BTreeMap1::new(1, "a");
assert_eq!(map.get(&1), Some(&"a"));
assert_eq!(map.get(&2), None);Run

pub fn contains_key<Q: ?Sized>(&self, key: &Q) -> bool where
    K: Borrow<Q>,
    Q: Ord
[src]

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

The key may be any borrowed form of the map's key type, but the ordering on the borrowed form must match the ordering on the key type.

Examples

Basic usage:

use map1::BTreeMap1;

let map = BTreeMap1::new(1, "a");
assert_eq!(map.contains_key(&1), true);
assert_eq!(map.contains_key(&2), false);Run

pub fn get_mut<Q: ?Sized>(&mut self, key: &Q) -> Option<&mut V> where
    K: Borrow<Q>,
    Q: Ord
[src]

Returns a mutable reference to the value corresponding to the key.

The key may be any borrowed form of the map's key type, but the ordering on the borrowed form must match the ordering on the key type.

Examples

Basic usage:

use map1::BTreeMap1;

let mut map = BTreeMap1::new(1, "a");
if let Some(x) = map.get_mut(&1) {
    *x = "b";
}
assert_eq!(map[&1], "b");Run

pub fn insert(&mut self, key: K, value: V) -> Option<V>[src]

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

Basic usage:

use map1::BTreeMap1;

let mut map = BTreeMap1::new(0, "foo");
assert_eq!(map.insert(37, "a"), None);

map.insert(37, "b");
assert_eq!(map.insert(37, "c"), Some("b"));
assert_eq!(map[&37], "c");Run

pub fn try_remove<Q: ?Sized>(
    &mut self,
    key: &Q
) -> Option<Result<V, BTreeEmptyError>> where
    K: Borrow<Q>,
    Q: Ord
[src]

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

The key may be any borrowed form of the map's key type, but the ordering on the borrowed form must match the ordering on the key type.

Examples

Basic usage:

use map1::{btree_map::BTreeEmptyError, btree_map_1};

let mut map = btree_map_1![(1, "a"), (2, "b")];
assert_eq!(map.try_remove(&1), Some(Ok("a")));
assert_eq!(map.try_remove(&1), None);
assert_eq!(map.try_remove(&2), Some(Err(BTreeEmptyError)));Run

Performance

Note that internally this implementation will search the tree twice in the case that the key exists. If calling Clone::clone on your keys is cheap, consider using try_remove_entry instead.

pub fn append(&mut self, other: &mut BTreeMap<K, V>)[src]

Moves all elements from other into Self, leaving other empty.

Examples

use {std::collections::BTreeMap, map1::btree_map_1};

let mut a = btree_map_1![
    (1, "a"),
    (2, "b"),
    (3, "c"),
];

let mut b = BTreeMap::new();
b.insert(3, "d");
b.insert(4, "e");
b.insert(5, "f");

a.append(&mut b);

assert_eq!(a.len().get(), 5);
assert_eq!(b.len(), 0);

assert_eq!(a[&1], "a");
assert_eq!(a[&2], "b");
assert_eq!(a[&3], "d");
assert_eq!(a[&4], "e");
assert_eq!(a[&5], "f");Run

pub fn range<T: ?Sized, R>(&self, range: R) -> Range<K, V> where
    T: Ord,
    K: Borrow<T>,
    R: RangeBounds<T>, 
[src]

Constructs a double-ended iterator over a sub-range of elements in the map. The simplest way is to use the range syntax min..max, thus range(min..max) will yield elements from min (inclusive) to max (exclusive). The range may also be entered as (Bound<T>, Bound<T>), so for example range((Excluded(4), Included(10))) will yield a left-exclusive, right-inclusive range from 4 to 10.

Panics

Panics if range start > end. Panics if range start == end and both bounds are Excluded.

Examples

Basic usage:

use map1::btree_map_1;
use std::ops::Bound::Included;

let map = btree_map_1![(3, "a"), (5, "b"), (8, "c")];
for (&key, &value) in map.range((Included(&4), Included(&8))) {
    println!("{}: {}", key, value);
}
assert_eq!(Some((&5, &"b")), map.range(4..).next());Run

pub fn range_mut<T: ?Sized, R>(&mut self, range: R) -> RangeMut<K, V> where
    T: Ord,
    K: Borrow<T>,
    R: RangeBounds<T>, 
[src]

Constructs a mutable double-ended iterator over a sub-range of elements in the map. The simplest way is to use the range syntax min..max, thus range(min..max) will yield elements from min (inclusive) to max (exclusive). The range may also be entered as (Bound<T>, Bound<T>), so for example range((Excluded(4), Included(10))) will yield a left-exclusive, right-inclusive range from 4 to 10.

Panics

Panics if range start > end. Panics if range start == end and both bounds are Excluded.

Examples

Basic usage:

use map1::BTreeMap1;

let mut map: BTreeMap1<&str, i32> = BTreeMap1::try_from_iter(
    ["Alice", "Bob", "Carol", "Cheryl"].iter().map(|&s| (s, 0))
).unwrap();
for (_, balance) in map.range_mut("B".."Cheryl") {
    *balance += 100;
}
for (name, balance) in &map {
    println!("{} => {}", name, balance);
}Run

pub fn entry(&mut self, key: K) -> Entry<K, V>[src]

Gets the given key's corresponding entry in the map for in-place manipulation.

Examples

Basic usage:

use map1::BTreeMap1;

let mut count: BTreeMap1<&str, usize> = BTreeMap1::new("a", 0);

// count the number of occurrences of letters in the vec
for x in vec!["a","b","a","c","a","b"] {
    *count.entry(x).or_insert(0) += 1;
}

assert_eq!(count["a"], 3);Run

pub fn split<Q: ?Sized + Ord>(self, key: &Q) -> (BTreeMap<K, V>, BTreeMap<K, V>) where
    K: Borrow<Q>, 
[src]

Splits the collection into two at the given key. Returns a tuple pair of BTreeMap; the first contains everything up to (but not including) the given key, and the second contains everything after (and including) the given key.

Examples

Basic usage:

use map1::btree_map_1;

let a = btree_map_1![
    (1, "a"),
    (2, "b"),
    (3, "c"),
    (17, "d"),
    (41, "e"),
];

let (a, b) = a.split(&3);

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

assert_eq!(a[&1], "a");
assert_eq!(a[&2], "b");

assert_eq!(b[&3], "c");
assert_eq!(b[&17], "d");
assert_eq!(b[&41], "e");Run

pub fn try_from_iter<I>(iter: I) -> Result<Self, BTreeEmptyError> where
    I: Iterator<Item = (K, V)>, 
[src]

Attempts to create a value from an iterator. Returns an error if the iterator yields no items.

impl<K: Ord, V> BTreeMap1<K, V>[src]

pub fn iter(&self) -> Iter<K, V>[src]

Gets an iterator over the entries of the map, sorted by key.

Examples

Basic usage:

use map1::btree_map_1;

let map = btree_map_1![
    (3, "c"),
    (2, "b"),
    (1, "a"),
];

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

let (first_key, first_value) = map.iter().next().unwrap();
assert_eq!((*first_key, *first_value), (1, "a"));Run

pub fn iter_mut(&mut self) -> IterMut<K, V>[src]

Gets a mutable iterator over the entries of the map, sorted by key.

Examples

Basic usage:

use map1::btree_map_1;

let mut map = btree_map_1![
    ("c", 3),
    ("b", 2),
    ("a", 1),
];

// add 10 to the value if the key isn't "a"
for (key, value) in map.iter_mut() {
    if key != &"a" {
        *value += 10;
    }
}Run

pub fn keys(&self) -> Keys<K, V>[src]

Gets an iterator over the keys of the map, in sorted order.

Examples

Basic usage:

use map1::btree_map_1;

let a = btree_map_1![
    (2, "b"),
    (1, "a"),
];

let keys: Vec<_> = a.keys().cloned().collect();
assert_eq!(keys, [1, 2]);Run

pub fn values(&self) -> Values<K, V>[src]

Gets an iterator over the values of the map, in order by key.

Examples

Basic usage:

use map1::btree_map_1;

let a = btree_map_1![
    (1, "hello"),
    (2, "goodbye"),
];

let values: Vec<&str> = a.values().cloned().collect();
assert_eq!(values, ["hello", "goodbye"]);Run

pub fn values_mut(&mut self) -> ValuesMut<K, V>[src]

Gets a mutable iterator over the values of the map, in order by key.

Examples

Basic usage:

use map1::btree_map_1;

let mut a = btree_map_1![
    (1, "hello".to_owned()),
    (2, "goodbye".to_owned()),
];

for value in a.values_mut() {
    value.push_str("!");
}

let values: Vec<String> = a.values().cloned().collect();
assert_eq!(values, [String::from("hello!"),
                    String::from("goodbye!")]);Run

pub fn len(&self) -> NonZeroUsize[src]

Returns the number of elements in the map.

Examples

Basic usage:

use map1::BTreeMap1;

let mut a = BTreeMap1::new(2, "b");
assert_eq!(a.len().get(), 1);
a.insert(1, "a");
assert_eq!(a.len().get(), 2);Run

Trait Implementations

impl<K: Eq, V: Eq> Eq for BTreeMap1<K, V>[src]

impl<K: Ord, V> Extend<(K, V)> for BTreeMap1<K, V>[src]

impl<'a, K: Ord + Copy, V: Copy> Extend<(&'a K, &'a V)> for BTreeMap1<K, V>[src]

impl<K: Ord, V> Into<BTreeMap<K, V>> for BTreeMap1<K, V>[src]

impl<K: Ord, V: Ord> Ord for BTreeMap1<K, V>[src]

fn max(self, other: Self) -> Self1.21.0[src]

Compares and returns the maximum of two values. Read more

fn min(self, other: Self) -> Self1.21.0[src]

Compares and returns the minimum of two values. Read more

fn clamp(self, min: Self, max: Self) -> Self[src]

🔬 This is a nightly-only experimental API. (clamp)

Restrict a value to a certain interval. Read more

impl<K: PartialEq, V: PartialEq> PartialEq<BTreeMap1<K, V>> for BTreeMap1<K, V>[src]

impl<K: Clone, V: Clone> Clone for BTreeMap1<K, V>[src]

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl<K: PartialOrd, V: PartialOrd> PartialOrd<BTreeMap1<K, V>> for BTreeMap1<K, V>[src]

impl<'a, K: 'a, V: 'a> IntoIterator for &'a BTreeMap1<K, V>[src]

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

The type of the elements being iterated over.

type IntoIter = <&'a BTreeMap<K, V> as IntoIterator>::IntoIter

Which kind of iterator are we turning this into?

impl<K, V> IntoIterator for BTreeMap1<K, V>[src]

type Item = (K, V)

The type of the elements being iterated over.

type IntoIter = <BTreeMap<K, V> as IntoIterator>::IntoIter

Which kind of iterator are we turning this into?

impl<'a, K: 'a, V: 'a> IntoIterator for &'a mut BTreeMap1<K, V>[src]

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

The type of the elements being iterated over.

type IntoIter = <&'a mut BTreeMap<K, V> as IntoIterator>::IntoIter

Which kind of iterator are we turning this into?

impl<K: Debug, V: Debug> Debug for BTreeMap1<K, V>[src]

impl<K: Ord, Q: ?Sized, V, '_> Index<&'_ Q> for BTreeMap1<K, V> where
    K: Borrow<Q>,
    Q: Ord
[src]

type Output = V

The returned type after indexing.

fn index(&self, key: &Q) -> &V[src]

Returns a reference to the value corresponding to the supplied key.

Panics

Panics if the key is not present in the BTreeMap1.

impl<K: Hash, V: Hash> Hash for BTreeMap1<K, V>[src]

fn hash_slice<H>(data: &[Self], state: &mut H) where
    H: Hasher
1.3.0[src]

Feeds a slice of this type into the given [Hasher]. Read more

impl<K: Ord, V> TryFrom<BTreeMap<K, V>> for BTreeMap1<K, V>[src]

type Error = BTreeEmptyError

The type returned in the event of a conversion error.

Auto Trait Implementations

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

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

Blanket Implementations

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<T> for T[src]

impl<I> IntoIterator for I where
    I: Iterator
[src]

type Item = <I as Iterator>::Item

The type of the elements being iterated over.

type IntoIter = I

Which kind of iterator are we turning this into?

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]