Seq

Enum Seq 

Source
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§

Source§

impl<'a, T: 'a> Seq<'a, T>

Seq method implementations

Source

pub fn head(&'a self) -> Option<&'a T>

Returns a reference to the head-element

Source

pub fn tail(&'a self) -> Option<&'a Seq<'_, T>>

Returns reference to the tail

Trait Implementations§

Source§

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

Source§

fn clone(&self) -> Seq<'a, T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a, T: Debug> Debug for Seq<'a, T>

Debug format of a sequence prints the head element only

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a, T> Default for Seq<'a, T>

By default a sequence is empty

Source§

fn default() -> Seq<'a, T>

Returns the “default value” for a type. Read more
Source§

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);
}
Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

type IntoIter = SeqIterator<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, T: PartialEq> PartialEq for Seq<'a, T>

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

Source§

fn eq(&self, other: &Seq<'a, T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<'a, T> UnwindSafe for Seq<'a, T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.