pub enum List<T> {
Cons(T, Box<List<T>>),
Nil,
}Expand description
An enum that represents a Cons list.
See the module level documentation for more.
Variants§
Implementations§
source§impl<T> List<T>
impl<T> List<T>
sourcepub fn val_as_ref(&self) -> List<&T>
👎Deprecated since 0.3.1: Any usage of val_as_ref can be replaced with as_ref.
pub fn val_as_ref(&self) -> List<&T>
sourcepub fn unwrap(self) -> ConsData<T>
pub fn unwrap(self) -> ConsData<T>
Returns the Cons value and next List,
consuming self.
Usage of this function is discouraged, as it may panic.
Instead, prefer to use pattern matching,
unwrap_or or unwrap_or_default.
Panics
Panics if self is Nil.
Examples
let x = List::new(5);
assert_eq!(x.unwrap(), (5, Nil));ⓘ
let x: List<i32> = Nil;
assert_eq!(x.unwrap(), (5, Nil)); // failssourcepub fn unwrap_or_default(self) -> ConsData<T>where
T: Default,
pub fn unwrap_or_default(self) -> ConsData<T>where T: Default,
Returns the contained Cons value and List, or a default.
Consumes self, and if self is Cons, returns the contained
value and list, otherwise, returns the default value
for T and Nil.
Examples
let x = List::new(3);
assert_eq!(x.unwrap_or_default(), (3, Nil));
let x: List<i32> = Nil;
assert_eq!(x.unwrap_or_default(), (0, Nil));sourcepub fn map<U, F>(self, f: F) -> List<U>where
F: FnOnce(T) -> U,
pub fn map<U, F>(self, f: F) -> List<U>where F: FnOnce(T) -> U,
Maps List<T> to List<U> by applying a function to the contained value
(if Cons, discarding the next value), or if Nil, returns Nil.
Examples
let x = List::new("Hello World".to_string());
let x_len = x.map(|s| s.len());
assert_eq!(x_len, List::new_val(11));
let x: List<String> = Nil;
let x_len = x.map(|s| s.len());
assert_eq!(x_len, Nil);sourcepub fn map_next<U, F>(self, f: F) -> List<U>where
F: FnOnce(ConsData<T>) -> ConsData<U>,
pub fn map_next<U, F>(self, f: F) -> List<U>where F: FnOnce(ConsData<T>) -> ConsData<U>,
Maps List<T> to List<U> by applying a function to the contained value
(if Cons), or returns Nil (if Nil).
Examples
// you can unpack with a pattern
let f = |(x, list)| (x + 1, list);
let x = List::new(5).map_next(f);
assert_eq!(x, Cons(6, Box::new(Nil)));
let x: List<i32> = Nil;
assert_eq!(x.map_next(f), Nil);source§impl<T> List<&T>
impl<T> List<&T>
source§impl<T> List<&mut T>
impl<T> List<&mut T>
Trait Implementations§
source§impl<T> FromIterator<T> for List<T>
impl<T> FromIterator<T> for List<T>
source§fn from_iter<U: IntoIterator<Item = T>>(iter: U) -> Self
fn from_iter<U: IntoIterator<Item = T>>(iter: U) -> Self
Creates a value from an iterator. Read more
source§impl<T: Clone> IntoIterator for List<T>
impl<T: Clone> IntoIterator for List<T>
source§impl<T: PartialEq> PartialEq<List<T>> for List<T>
impl<T: PartialEq> PartialEq<List<T>> for List<T>
impl<T> StructuralPartialEq for List<T>
Auto Trait Implementations§
impl<T> RefUnwindSafe for List<T>where T: RefUnwindSafe,
impl<T> Send for List<T>where T: Send,
impl<T> Sync for List<T>where T: Sync,
impl<T> Unpin for List<T>where T: Unpin,
impl<T> UnwindSafe for List<T>where T: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more