Struct IndexMap

Source
pub struct IndexMap<T> { /* private fields */ }
Expand description

A map of usize to value, which allows efficient O(1) inserts, O(1) indexing and O(1) removal.

See crate level documentation for more information.

Implementations§

Source§

impl<T> IndexMap<T>

Source

pub fn keys(&self) -> Keys<'_, T>

An iterator visiting all keys in ascending order. The iterator element type is usize.

§Examples
use index_map::IndexMap;

let mut map = IndexMap::new();
map.insert("a");
map.insert("b");
map.insert("c");

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

pub fn values(&self) -> Values<'_, T>

An iterator visiting all values in ascending order of their keys. The iterator element type is &T.

§Examples
use index_map::IndexMap;

let mut map = IndexMap::new();
map.insert("a");
map.insert("b");
map.insert("c");

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

pub fn values_mut(&mut self) -> ValuesMut<'_, T>

An iterator visiting all values mutably in ascending order of their keys. The iterator element type is &mut T.

§Examples
use index_map::IndexMap;

let mut map = IndexMap::new();
map.insert(2);
map.insert(4);
map.insert(6);

for val in map.values_mut() {
    *val *= 2;
}

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

pub fn iter(&self) -> Iter<'_, T>

An iterator visiting all key-value pairs in ascending order of keys. The iterator element type is (usize, &T).

§Examples
use index_map::IndexMap;

let mut map = IndexMap::new();
map.insert("a");
map.insert("b");
map.insert("c");

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

pub fn iter_mut(&mut self) -> IterMut<'_, T>

An iterator visiting all key-value pairs in ascending order of keys, with mutable references to the values. The iterator element type is (usize, &mut T).

§Examples
use index_map::IndexMap;

let mut map = IndexMap::new();
map.insert(2);
map.insert(4);
map.insert(6);

// Update all values
for (_, val) in map.iter_mut() {
    *val *= 2;
}

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

pub fn drain(&mut self) -> Drain<'_, T>

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

§Examples
use index_map::IndexMap;

let mut a = IndexMap::new();
a.insert("a");
a.insert("b");

let mut iter = a.drain();
assert_eq!(iter.next(), Some((0, "a")));
assert_eq!(iter.next(), Some((1, "b")));
assert_eq!(iter.next(), None);

assert!(a.is_empty());
Source§

impl<T> IndexMap<T>

Source

pub fn new() -> Self

Creates a new IndexMap.

It initially has a capacity of 0, and won’t allocate until first inserted into.

§Examples
use index_map::IndexMap;
let mut map: IndexMap<&str> = IndexMap::new();
Source

pub fn with_capacity(capacity: usize) -> Self

Creates an empty IndexMap with the specified capacity.

The map will be able to hold at least capacity elements without reallocating. If capacity is 0, the map will not allocate.

§Examples
use index_map::IndexMap;
let mut map: IndexMap<&str> = IndexMap::with_capacity(10);
Source

pub fn capacity(&self) -> usize

Returns the number of elements map can hold without reallocating.

§Examples
use index_map::IndexMap;
let mut map: IndexMap<&str> = IndexMap::with_capacity(10);
assert!(map.capacity() >= 10);
Source

pub fn len(&self) -> usize

Returns the number of elements present in the map.

§Examples
use index_map::IndexMap;
let mut map = IndexMap::new();
assert_eq!(map.len(), 0);
map.insert("a");
assert_eq!(map.len(), 1);
Source

pub fn is_empty(&self) -> bool

Returns true if the map is empty.

§Examples
use index_map::IndexMap;
let mut map = IndexMap::new();
assert!(map.is_empty());
map.insert("a");
assert!(!map.is_empty());
Source

pub fn clear(&mut self)

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

§Examples
use index_map::IndexMap;
let mut map = IndexMap::new();

map.insert("a");
map.clear();

assert!(map.is_empty());
Source

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

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

§Panics

Panics if the new capacity exceeds isize::MAX bytes.

§Examples
use index_map::IndexMap;
let mut map: IndexMap<&str> = IndexMap::new();
map.reserve(10);
assert!(map.capacity() >= 10);
Source

pub fn shrink_to_fit(&mut self)

Shrinks the capacity of the map as much as possible. It will drop down as much as possible while maintaining the internal rules and possibly leaving some space to keep keys valid.

§Examples
use index_map::IndexMap;
let mut map = IndexMap::with_capacity(100);
map.insert("a");
map.insert("b");
assert!(map.capacity() >= 100);
map.shrink_to_fit();
assert!(map.capacity() >= 2);
Source

pub fn contains_key(&self, index: usize) -> bool

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

§Examples
use index_map::IndexMap;

let mut map = IndexMap::new();
map.insert("a");
assert_eq!(map.contains_key(0), true);
assert_eq!(map.contains_key(1), false);
Source

pub fn insert(&mut self, value: T) -> usize

Inserts a value into the map, returning the generated key, for it.

§Examples
use index_map::IndexMap;

let mut map = IndexMap::new();
assert_eq!(map.insert("a"), 0);
assert_eq!(map.is_empty(), false);

let b = map.insert("b");
assert_eq!(map[b], "b");
Source

pub fn remove(&mut self, index: usize) -> Option<T>

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

§Examples
use index_map::IndexMap;

let mut map = IndexMap::new();
let a = map.insert("a");
assert_eq!(map.remove(a), Some("a"));
assert_eq!(map.remove(a), None);
Source

pub fn remove_entry(&mut self, index: usize) -> Option<(usize, T)>

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

§Examples
use index_map::IndexMap;

let mut map = IndexMap::new();
let a = map.insert("a");
assert_eq!(map.remove_entry(a), Some((0, "a")));
assert_eq!(map.remove(a), None);
Source

pub fn get(&self, index: usize) -> Option<&T>

Returns a reference to the value corresponding to the key.

§Examples
use index_map::IndexMap;

let mut map = IndexMap::new();
map.insert("a");
assert_eq!(map.get(0), Some(&"a"));
assert_eq!(map.get(1), None);
Source

pub fn get_key_value(&self, index: usize) -> Option<(usize, &T)>

Returns the key-value pair corresponding to the key.

§Examples
use index_map::IndexMap;

let mut map = IndexMap::new();
map.insert("a");
assert_eq!(map.get_key_value(0), Some((0, &"a")));
assert_eq!(map.get_key_value(1), None);
Source

pub fn get_mut(&mut self, index: usize) -> Option<&mut T>

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

§Examples
use index_map::IndexMap;

let mut map = IndexMap::new();
let a = map.insert("a");
if let Some(x) = map.get_mut(a) {
    *x = "b";
}
assert_eq!(map[a], "b");
Source

pub fn retain<P>(&mut self, predicate: P)
where P: FnMut(usize, &mut T) -> bool,

Retains only the elements specified by the predicate.

In other words, remove all pairs (k, v) such that f(k, &mut v) returns false.

§Examples
use index_map::IndexMap;

let mut map = IndexMap::new();
for i in 0..6 {
    map.insert(i*2);
}
map.retain(|k, _| k % 2 == 0);
assert_eq!(map.len(), 3);

Trait Implementations§

Source§

impl<T: Clone> Clone for IndexMap<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for IndexMap<T>

Source§

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

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

impl<T> Default for IndexMap<T>

Source§

fn default() -> Self

Creates an empty IndexMap, same as calling new.

Source§

impl<T> Index<usize> for IndexMap<T>

Source§

fn index(&self, key: usize) -> &T

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

§Panics

Panics if the key is not present in the IndexMap.

Source§

type Output = T

The returned type after indexing.
Source§

impl<T> IndexMut<usize> for IndexMap<T>

Source§

fn index_mut(&mut self, key: usize) -> &mut T

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

§Panics

Panics if the key is not present in the IndexMap.

Source§

impl<'a, T> IntoIterator for &'a IndexMap<T>

Source§

type Item = (usize, &'a T)

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, T>

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

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, T> IntoIterator for &'a mut IndexMap<T>

Source§

type Item = (usize, &'a mut T)

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a, T>

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

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T> IntoIterator for IndexMap<T>

Source§

type Item = (usize, T)

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<T>

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

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T: Ord> Ord for IndexMap<T>

Source§

fn cmp(&self, other: &IndexMap<T>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T: PartialEq> PartialEq for IndexMap<T>

Source§

fn eq(&self, other: &IndexMap<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: PartialOrd> PartialOrd for IndexMap<T>

Source§

fn partial_cmp(&self, other: &IndexMap<T>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T: Eq> Eq for IndexMap<T>

Source§

impl<T> StructuralPartialEq for IndexMap<T>

Auto Trait Implementations§

§

impl<T> Freeze for IndexMap<T>

§

impl<T> RefUnwindSafe for IndexMap<T>
where T: RefUnwindSafe,

§

impl<T> Send for IndexMap<T>
where T: Send,

§

impl<T> Sync for IndexMap<T>
where T: Sync,

§

impl<T> Unpin for IndexMap<T>
where T: Unpin,

§

impl<T> UnwindSafe for IndexMap<T>
where T: 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.