Enum seq::Seq [] [src]

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

The data type Seq is a generic data container providing linked list functionality. Seq is a sequence of data of type T and lifetime 'a.

Either a sequence is Empty or a sequence is a construction of a new value (head or first (ft)) on-top of another sequence (the tail or rest (rt)). The lifetime of the tail must be at least as long as the one of the head. Two kind of constructions are possible, depending on location of tail in memory-heap or on stack. rust pub enum Seq<'a, T: 'a> { Empty, ConsRef(T, &'a Seq<'a, T>), ConsOwn(T, Box<Seq<'a, T>>), } The semantic of these variants is as follows: * Empty: The empty sequence <> * ConsRef(head, tail): Constructs a new sequence with head being the first element and tail referencing another, borrowed sequence. This variant permits construction of sequences using stack-allocated data solely. * ConsOwn(head, boxedtail): Constructs a new sequence with head being the first element and boxedtail referencing another, owned, boxed sequence. Here the tail is residing in heap allocated memory. This variant permits construction of sequences using heap-allocated dynamic data.

These variants may be combined with each other, representing a mixture of borrowed and owned elements. The memory safety feature of Rust allows automated and correct management of lifetime of each element of the sequence.

The lifetime of each element of the sequence depends on the function-context it has been added to the top of the sequence; Empty is the element with longest lifetime.

In first place, the container Seq is intended as lightweight, dynamic, stack-allocated, linked list for use cases such as traversing tree-structures without any dynamic memory-allocation (heap) involved.

The sequence type Seq implements the trait IntoIterator, enabling the usage of Rust's iterator framework.

A sequence can be a mixture of borrowed and owned data elements: let s0: &Seq<u32> = empty(); let s1: Seq<u32> = Seq::ConsRef(0, s0); let s2: Box<Seq<u32>> = Box::new(Seq::ConsRef(1, &s1)); let s3: Box<Seq<u32>> = Box::new(Seq::ConsOwn(2, s2)); let s4: Seq<u32> = Seq::ConsOwn(Data(3, s3); Pattern-matching can be used to de-construct a sequence. fn head(seq: &Seq<u32>) -> Option<u32> { match self.cur { &Seq::Empty => Option::None, &Seq::ConsRef(ref ft, ref rt) => Option::Some(*ft), &Seq::ConsOwn(ref ft, ref rt) => Option::Some(*ft) }

Variants

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.

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

A sequence can be processed using an iterator: let sequence = empty<u32>(); let sum = sequence.into_iter().fold(0, ops::Add::add);

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