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>
pub fn val_as_ref(&self) -> List<&T>
sourcepub fn as_ref(&self) -> List<&T>
pub fn as_ref(&self) -> List<&T>
Converts from &List<T> to List<&T>. If self is Cons,
a List containing a reference to val and next is returned.
To ignore next, use val_as_ref.
sourcepub fn unwrap(self) -> (T, List<T>)
pub fn unwrap(self) -> (T, List<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_val(5);
assert_eq!(x.unwrap(), (5, Nil));ⓘ
let x: List<i32> = Nil;
assert_eq!(x.unwrap(), (5, Nil)); // failssourcepub fn unwrap_or_default(self) -> (T, List<T>)where
T: Default,
pub fn unwrap_or_default(self) -> (T, List<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_val(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_val("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);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