pub enum Seq<'a, T: 'a> {
Empty,
ConsRef(T, &'a Seq<'a, T>),
ConsOwn(T, Box<Seq<'a, T>>),
}Expand description
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
Implementations§
Trait Implementations§
Source§impl<'a, T: Debug> Debug for Seq<'a, T>
Debug format of a sequence prints the head element only
impl<'a, T: Debug> Debug for Seq<'a, T>
Debug format of a sequence prints the head element only
Source§impl<'a, T: 'a> IntoIterator for &'a Seq<'a, T>
A sequence implements the IntoIterator trait
impl<'a, T: 'a> IntoIterator for &'a Seq<'a, T>
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);
}