Enum im::list::List [] [src]

pub enum List<A> {
    // some variants omitted
}

A list of elements of type A.

Methods

impl<A> List<A>
[src]

Construct an empty list.

Construct a list with a single element.

Test whether a list is empty.

Time: O(1)

Construct a list with a new value prepended to the front of the current list.

Time: O(1)

Get the first element of a list.

If the list is empty, None is returned.

Time: O(1)

Get the tail of a list.

The tail means all elements in the list after the first item (the head). If the list only has one element, the result is an empty list. If the list is empty, the result is None.

Time: O(1)

Get the head and the tail of a list.

This function performs both the head function and the tail function in one go, returning a tuple of the head and the tail, or None if the list is empty.

Examples

This can be useful when pattern matching your way through a list:

fn walk_through_list<A>(list: &List<A>) where A: Debug {
    match list.uncons() {
        None => (),
        Some((ref head, ref tail)) => {
            print!("{:?}", head);
            walk_through_list(tail)
        }
    }
}

Time: O(1)

Get the length of a list.

This operation is instant, because cons cells store the length of the list they're the head of.

Time: O(1)

Examples

assert_eq!(5, list![1, 2, 3, 4, 5].length());

impl List<i32>
[src]

Construct a list of numbers between from and to inclusive.

Examples

assert_eq!(
  List::range(1, 5),
  list![1, 2, 3, 4, 5]
);

impl<A> List<A> where
    A: Clone + Ord
[src]

Insert an item into a sorted list.

Constructs a new list with the new item inserted before the first item in the list which is larger than the new item, as determined by the Ord trait.

Time: O(n)

Examples

assert_eq!(
  list![2, 4, 6].insert(&5).insert(&1).insert(&3),
  list![1, 2, 3, 4, 5, 6]
);

Sort a list.

Time: O(n log n)

Examples

assert_eq!(
  list![2, 8, 1, 6, 3, 7, 5, 4].sort(),
  List::range(1, 8)
);

impl<A> List<A> where
    A: Clone
[src]

Construct a list from a slice of items.

Time: O(n)

Append the list right to the end of the current list.

Time: O(n)

Examples

assert_eq!(
  list![1, 2, 3].append(&list![7, 8, 9]),
  list![1, 2, 3, 7, 8, 9]
);

Construct a list which is the reverse of the current list.

Time: O(n)

Examples

assert_eq!(
  list![1, 2, 3, 4, 5].reverse(),
  list![5, 4, 3, 2, 1]
);

Get an iterator over a list.

Sort a list using a comparator function.

Time: O(n log n)

Trait Implementations

impl<A> Clone for List<A>
[src]

Clone a list.

Cons cells use Arc behind the scenes, so this is no more expensive than cloning an Arc reference.

Time: O(1)

Performs copy-assignment from source. Read more

impl<A> Default for List<A>
[src]

Default for lists is the empty list.

impl<A> IntoIterator for List<A> where
    A: Clone
[src]

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

impl<A> FromIterator<A> for List<A> where
    A: Clone
[src]

Creates a value from an iterator. Read more

impl<'a, A> FromIterator<&'a A> for List<A> where
    A: 'a + Clone
[src]

Creates a value from an iterator. Read more

impl<'a, A> From<&'a [A]> for List<A> where
    A: Clone
[src]

Performs the conversion.

impl<A> PartialEq for List<A> where
    A: PartialEq + Clone
[src]

Test if two lists are equal.

This could potentially be an expensive operation, as we need to walk both lists to test for equality. We can very quickly determine equality if both lists are references to the same cons cell (they're equal) or if the lists have different lengths (can't be equal). Otherwise, we walk the lists to compare values.

Time: O(n)

This method tests for !=.

impl<A> Eq for List<A> where
    A: Eq + Clone
[src]

impl<A> Hash for List<A> where
    A: Clone + Hash
[src]

Feeds this value into the state given, updating the hasher as necessary.

Feeds a slice of this type into the state provided.

impl<A> AsRef<List<A>> for List<A>
[src]

Performs the conversion.

impl<A> Debug for List<A> where
    A: Debug
[src]

Formats the value using the given formatter.