Crate seq [−] [src]
The module seq
provides the lightweight, generic sequence container Seq
for unmovable data
and is embedded into the program during compile time. Elements of Seq
are
stacked on top of each other.
Initially a sequence is empty. A longer sequence is constructed attaching a new head to the existing sequence, representing the tail.
Multiple sequences may share the same tail, permitting memory-efficient organisation of hierarchical data.
Put this in your Cargo.toml:
## Cargo.toml file
[dependencies]
seq = "0.5"
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); }
Macros
seqdef |
The seqdef! macro defines a stack-allocated sequence variable for the speficied data list, the last data item in the list will be the top most in the sequence. |
Structs
SeqIterator |
The sequence iterator representation |
Enums
Seq |
A single-ended, growable, unmovable queue of data, linking constant data with dynamic data. |
Functions
empty |
Function returns static reference to empty list |