13 releases (breaking)

Uses old Rust 2015

0.12.0 Feb 5, 2017
0.11.0 Oct 13, 2016
0.10.0 Jan 19, 2016
0.8.0 Dec 29, 2015
0.0.1 Mar 18, 2015

#773 in Data structures

Download history 46/week @ 2021-01-15 61/week @ 2021-01-22 72/week @ 2021-01-29 111/week @ 2021-02-05 125/week @ 2021-02-12 85/week @ 2021-02-19 69/week @ 2021-02-26 71/week @ 2021-03-05 38/week @ 2021-03-12 77/week @ 2021-03-19 81/week @ 2021-03-26 37/week @ 2021-04-02 47/week @ 2021-04-09 40/week @ 2021-04-16 2/week @ 2021-04-23 15/week @ 2021-04-30

227 downloads per month


976 lines


Experimental collection traits for Rust.

Documentation is available at https://apasel422.github.io/eclectic/eclectic.

To use eclectic with Cargo, add this to Cargo.toml:

eclectic = "0.11"

and this to the crate root:

extern crate eclectic;


Collection traits for generic programming.

The principal traits in this library are:

When combined with these traits, two marker traits enable the use of additional operations:

Marker Operations Analogous Type
(none) Read-only access to a collection and its items &[T]
Mutate Write access to a collection's items &mut [T]
AddRemove Insertion and removal of a collection's items &mut Vec<T>

Generic code should specify only those bounds that are needed for its operation, but may specify additional bounds for future compatibility. Generic code should also use the collection traits with a ?Sized bound in order to support slices and trait objects whenever possible.


Insertion sort:

use eclectic::{List, Mutate};

fn insertion_sort<L: ?Sized + List + Mutate>(list: &mut L) where L::Item: Ord {
    for i in 1..list.len() { // `len` is defined on `Collection`, a supertrait of `List`
        let mut j = i;

        while j > 0 && list.get(j) < list.get(j - 1) {
            list.swap(j, j - 1); // the `Mutate` bound on `L` enables the use of `List::swap`
            j -= 1;

use std::collections::VecDeque;

let mut vec = vec!['c', 'a', 'e', 'd', 'b'];
let mut vec_deque: VecDeque<_> = vec.iter().cloned().collect();

insertion_sort(&mut vec);
assert_eq!(vec, ['a', 'b', 'c', 'd', 'e']);

insertion_sort(&mut vec_deque);
assert!(vec_deque.iter().eq(&['a', 'b', 'c', 'd', 'e']));

A Note on Trait Objects

A number of trait methods in this crate return a Box<Iterator>, which requires unnecessary heap allocation and opaqueness (e.g. erasure of traits like Clone and DoubleEndedIterator). This is to make up for the (hopefully temporary) inability to define higher-kinded associated types like:

trait Collection {
    type Drain<'a>: 'a + Iterator<Item = Self::Item>;

    fn drain<'a>(&'a mut self) -> Self::Drain<'a> where Self: AddRemove;

If Rust acquires such types, the iterator- and entry-returning methods will be changed to use them. Maps. Sets.

No runtime deps


  • nightly