Trait Collection

Source
pub trait Collection<T> {
    // Required methods
    fn push(&mut self, elem: T);
    fn extend_from_slice(&mut self, elems: &[T])
       where T: Clone;
    fn extend<I>(&mut self, elems: I)
       where I: IntoIterator<Item = T>;

    // Provided method
    fn reserve(&mut self, _size: usize) { ... }
}
Expand description

This trait is meant to abstract any kind of collection (i.e. Vec, HashSet).

This is particularly helpful when you want particular behavior when inserting elements, the Counter struct is a good example of a custom implementation of the Collection trait, it is used to only count the number of elements of a set operation.

Required Methods§

Source

fn push(&mut self, elem: T)

Insert one element into the collection.

Source

fn extend_from_slice(&mut self, elems: &[T])
where T: Clone,

Extend the collection by cloning the elements.

Source

fn extend<I>(&mut self, elems: I)
where I: IntoIterator<Item = T>,

Extend the collection by inserting the elements from the given Iterator.

Provided Methods§

Source

fn reserve(&mut self, _size: usize)

Reserve enough space in the collection for size elements.

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<T> Collection<T> for Vec<T>

Source§

fn push(&mut self, elem: T)

Source§

fn extend_from_slice(&mut self, elems: &[T])
where T: Clone,

Source§

fn extend<I>(&mut self, elems: I)
where I: IntoIterator<Item = T>,

Source§

fn reserve(&mut self, size: usize)

Source§

impl<T: Ord> Collection<T> for BTreeSet<T>

Source§

fn push(&mut self, elem: T)

Source§

fn extend_from_slice(&mut self, elems: &[T])
where T: Clone,

Source§

fn extend<I>(&mut self, elems: I)
where I: IntoIterator<Item = T>,

Source§

impl<T: Hash + Eq> Collection<T> for HashSet<T>

Source§

fn push(&mut self, elem: T)

Source§

fn extend_from_slice(&mut self, elems: &[T])
where T: Clone,

Source§

fn extend<I>(&mut self, elems: I)
where I: IntoIterator<Item = T>,

Source§

fn reserve(&mut self, size: usize)

Implementors§