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
Empty
The empty sequence
ConsRef(T, &'a Seq<'a, T>)
Constructing a sequence with head data and reference to a tail
ConsOwn(T, Box<Seq<'a, T>>)
Constructing a sequence with head data and reference to boxed tail
Methods
impl<'a, T: 'a> Seq<'a, T>
[src]
Seq method implementations
pub fn head(&'a self) -> Option<&'a T>
[src]
Returns a reference to the head-element
pub fn tail(&'a self) -> Option<&'a Seq<T>>
[src]
Returns reference to the tail
Trait Implementations
impl<'a, T: Clone + 'a> Clone for Seq<'a, T>
[src]
fn clone(&self) -> Seq<'a, T>
[src]
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
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
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.
fn eq(&self, other: &Seq<'a, T>) -> bool
[src]
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, other: &Rhs) -> bool
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
fn fmt(&self, f: &mut Formatter) -> Result
[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); }