Trait ExtendWithCapacity

Source
pub trait ExtendWithCapacity<T>: Extend<T> {
    // Required method
    fn with_capacity(capacity: usize) -> Self;
}
Expand description

A trait for collections that can be pre-allocated with specific capacity and extended with elements.

This extends the standard Extend trait to provide a uniform interface for collection types that support capacity pre-allocation.

Pre-allocating capacity can improve performance when the number of elements is known in advance. Due to the fact that in Rust iterators, size_hint() might return (0, None), pre-allocating capacity can be particularly useful.

§Implementors

  • alloc collections: Vec<T>, String, VecDeque<T>, BinaryHeap<T>
  • std types (with std feature): OsString, HashMap, HashSet, PathBuf
  • AHash collections (with ahash & std features): AHashMap, AHashSet

Required Methods§

Source

fn with_capacity(capacity: usize) -> Self

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<K, S> ExtendWithCapacity<K> for HashSet<K, S>
where K: Eq + Hash, S: BuildHasher + Default,

Available on crate feature std only.
Source§

fn with_capacity(capacity: usize) -> Self

Source§

impl<K, S> ExtendWithCapacity<K> for IndexSet<K, S>
where K: Eq + Hash, S: BuildHasher + Default,

Available on crate feature indexmap only.
Source§

fn with_capacity(capacity: usize) -> Self

Source§

impl<K, V, S> ExtendWithCapacity<(K, V)> for HashMap<K, V, S>
where K: Eq + Hash, S: BuildHasher + Default,

Available on crate feature std only.
Source§

fn with_capacity(capacity: usize) -> Self

Source§

impl<K, V, S> ExtendWithCapacity<(K, V)> for IndexMap<K, V, S>
where K: Eq + Hash, S: BuildHasher + Default,

Available on crate feature indexmap only.
Source§

fn with_capacity(capacity: usize) -> Self

Source§

impl<K: Eq + Hash> ExtendWithCapacity<K> for AHashSet<K>

Available on crate feature ahash only.
Source§

fn with_capacity(capacity: usize) -> Self

Source§

impl<K: Eq + Hash, V> ExtendWithCapacity<(K, V)> for AHashMap<K, V>

Available on crate feature ahash only.
Source§

fn with_capacity(capacity: usize) -> Self

Source§

impl<T> ExtendWithCapacity<T> for VecDeque<T>

Source§

fn with_capacity(capacity: usize) -> Self

Source§

impl<T> ExtendWithCapacity<T> for String
where String: Extend<T>,

Source§

fn with_capacity(capacity: usize) -> Self

Source§

impl<T> ExtendWithCapacity<T> for Vec<T>

Source§

fn with_capacity(capacity: usize) -> Self

Source§

impl<T> ExtendWithCapacity<T> for OsString
where OsString: Extend<T>,

Available on crate feature std only.
Source§

fn with_capacity(capacity: usize) -> Self

Source§

impl<T: Ord> ExtendWithCapacity<T> for BinaryHeap<T>

Source§

fn with_capacity(capacity: usize) -> Self

Source§

impl<T: AsRef<Path>> ExtendWithCapacity<T> for PathBuf

Available on crate feature std only.
Source§

fn with_capacity(capacity: usize) -> Self

Implementors§