Enum seq::Seq [] [src]

pub enum Seq<'a, T: 'a> {
    Empty,
    ConsRef(T, &'a Seq<'a, T>),
    ConsOwn(T, Box<Seq<'a, T>>),
}

A single-ended, growable, unmovable queue of data, linking constant data with dynamic data.

The "default" usage of this type as a queue is to use Empty or ConsRef to construct a queue, and head and tail to deconstruct a queue into head and remaining tail of a sequence.

Examples

Constructing two sequences seq1 as [1,0] and seq2 as [2,1,0], sharing data with seq1

use seq::Seq;
// constructing the sequence 'seq1'
const seq1: Seq<i32> = Seq::ConsRef(1, &Seq::ConsRef(0, &Seq::Empty));

// construction the sequence 'seq2' sharing data with 'seq1'
const seq2: Seq<i32> = Seq::ConsRef(2, &seq1);

Deconstructing a sequence

use seq::Seq;

fn print_head<'a>(seq: &'a Seq<i32>) {
   println!("head {}", seq.head().unwrap());
}

Extend an existing sequence. Note the lifetime of the return type matches the one of the tail.

use seq::Seq;

fn extend<'a>(head: i32, tail: &'a Seq<i32>) -> Seq<'a, i32> {
   return Seq::ConsRef(head, tail);
}

Extend an existing sequence with dynamic element residing in heap-memory

use seq::Seq;

fn extend_boxed<'a>(head: i32, tail: &'a Seq<i32>) -> Box<Seq<'a, i32>> {
   return Box::new(Seq::ConsRef(head, tail));
}

Iterate a sequence

use seq::Seq;

fn sum_up(seq: &Seq<i32>) -> i32 {
   return seq.into_iter().fold(0, |x, y| x + y);
}

Variants

The empty sequence

Constructing a sequence with head data and reference to a tail

Constructing a sequence with head data and reference to boxed tail

Methods

impl<'a, T: 'a> Seq<'a, T>
[src]

Seq method implementations

[src]

Returns a reference to the head-element

[src]

Returns reference to the tail

Trait Implementations

impl<'a, T: Clone + 'a> Clone for Seq<'a, T>
[src]

[src]

Returns a copy of the value. Read more

1.0.0
[src]

Performs copy-assignment from source. Read more

impl<'a, T> Default for Seq<'a, T>
[src]

By default a sequence is empty

[src]

Returns the "default value" for a type. Read more

impl<'a, T: PartialEq> PartialEq for Seq<'a, T>
[src]

Two sequences of type T are equal in case of identical length and sequence of equal data elements.

[src]

This method tests for self and other values to be equal, and is used by ==. Read more

1.0.0
[src]

This method tests for !=.

impl<'a, T: Debug> Debug for Seq<'a, T>
[src]

Debug format of a sequence prints the head element only

[src]

Formats the value using the given formatter. Read more

impl<'a, T: 'a> IntoIterator for &'a Seq<'a, T>
[src]

A sequence implements the IntoIterator trait

Example

use seq::Seq;

fn sum_up(seq: &Seq<i32>) -> i32 {
   return seq.into_iter().fold(0, |x, y| x + y);
}

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

[src]

Creates an iterator from a value. Read more

Auto Trait Implementations

impl<'a, T> Send for Seq<'a, T> where
    T: Send + Sync

impl<'a, T> Sync for Seq<'a, T> where
    T: Sync