pub struct List<T> { /* private fields */ }Expand description
A singly linked immutable list. See the module-level documentation for more.
Implementations§
Source§impl<T> List<T>
impl<T> List<T>
Sourcepub fn prepend(&self, element: T) -> List<T>
pub fn prepend(&self, element: T) -> List<T>
Prepends a given element to the list, returning a copy of the list with the added element.
§Examples
use cons_rs::immutable::List;
let list = List::new().prepend(1);
assert_eq!(list.head(), Some(&1));Sourcepub fn tail(&self) -> List<T>
pub fn tail(&self) -> List<T>
Returns a copy of the list with the first element removed.
§Examples
use cons_rs::immutable::List;
let list = List::new().prepend(1);
assert_eq!(list.tail().head(), None);
let list = List::new().prepend(1).prepend(2);
assert_eq!(list.tail().head(), Some(&1));Sourcepub fn head(&self) -> Option<&T>
pub fn head(&self) -> Option<&T>
Returns a reference to the first element in the list, if it exists.
§Examples
use cons_rs::immutable::List;
let list = List::new().prepend(1);
assert_eq!(list.head(), Some(&1));Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the length of the list.
§Examples
use cons_rs::immutable::List;
let list = List::new();
assert_eq!(list.len(), 0);
let list = list.prepend(3);
assert_eq!(list.len(), 1);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Checks if the list is empty.
§Examples
use cons_rs::immutable::List;
let list = List::new();
assert!(list.is_empty());
let list = list.prepend(1);
assert!(!list.is_empty());Sourcepub fn iter(&self) -> Iter<'_, T> ⓘ
pub fn iter(&self) -> Iter<'_, T> ⓘ
Creates an iterator that yields references to all the elements in the list.
§Examples
use cons_rs::immutable::List;
let list = List::new().prepend(1).prepend(2);
let mut iter = list.iter();
assert_eq!(iter.next(), Some(&2));
assert_eq!(iter.next(), Some(&1));
assert_eq!(iter.next(), None);Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for List<T>
impl<T> RefUnwindSafe for List<T>where
T: RefUnwindSafe,
impl<T> !Send for List<T>
impl<T> !Sync for List<T>
impl<T> Unpin for List<T>
impl<T> UnwindSafe for List<T>where
T: RefUnwindSafe,
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