pub struct VList<T> { /* private fields */ }Expand description
A dynamic array implementation backed by a VList.
§Example
use rust_dsa::VList;
// First, we create a new list.
let mut list = VList::new();
// Then we can push values.
list.push(4);
list.push(1);
list.push(3);
// We can get values.
assert_eq!(list.get(0), Some(&4));
assert_eq!(list.get(1), Some(&1));
assert_eq!(list.last(), Some(&3));
// And pop from them off.
assert_eq!(list.pop(), Some(3));
assert_eq!(list.pop(), Some(1));
assert_eq!(list.pop(), Some(4));
assert!(list.is_empty());
// We can also crate lists from arrays.
let list_a = VList::from(['v', 'l', 'i', 's', 't']);
// And iterators.
let list_b: VList<_> = "vlist".chars().collect();
// We can iterate over a list.
for (a, b) in std::iter::zip(list_a, list_b) {
assert_eq!(a, b);
}Implementations§
Source§impl<T> VList<T>
impl<T> VList<T>
Sourcepub fn push(&mut self, value: T)
pub fn push(&mut self, value: T)
Pushes a value onto the end of the list.
§Example
use rust_dsa::VList;
let mut list = VList::new();
list.push(5);
assert_eq!(list.last(), Some(&5));Sourcepub fn pop(&mut self) -> Option<T>
pub fn pop(&mut self) -> Option<T>
Removes and returns the value at the end of the list, or None if the list is empty.
§Example
use rust_dsa::VList;
let mut list = VList::new();
list.push(5);
assert_eq!(list.pop(), Some(5));
assert_eq!(list.pop(), None);Sourcepub fn last(&mut self) -> Option<&T>
pub fn last(&mut self) -> Option<&T>
Returns a reference to value at the end of the list, or None if the list is empty.
§Example
use rust_dsa::VList;
let mut list = VList::from([5]);
assert_eq!(list.last(), Some(&5));
list.pop();
assert_eq!(list.last(), None);Sourcepub fn get(&self, index: usize) -> Option<&T>
pub fn get(&self, index: usize) -> Option<&T>
Returns a reference to the value at position index if one exists.
§Example
use rust_dsa::VList;
let list = VList::from(['a', 'b', 'c', 'd']);
assert_eq!(list.get(0), Some(&'a'));
assert_eq!(list.get(1), Some(&'b'));
assert_eq!(list.get(2), Some(&'c'));
assert_eq!(list.get(3), Some(&'d'));
assert_eq!(list.get(4), None);Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the length of the list.
§Example
use rust_dsa::VList;
let full: VList<_> = (0..10).collect();
assert_eq!(full.len(), 10);
let empty: VList<i32> = VList::new();
assert_eq!(empty.len(), 0);Trait Implementations§
Source§impl<T> FromIterator<T> for VList<T>
impl<T> FromIterator<T> for VList<T>
Source§impl<'a, T> IntoIterator for &'a VList<T>
impl<'a, T> IntoIterator for &'a VList<T>
Source§impl<T> IntoIterator for VList<T>
impl<T> IntoIterator for VList<T>
impl<T: Eq> Eq for VList<T>
Auto Trait Implementations§
impl<T> Freeze for VList<T>
impl<T> RefUnwindSafe for VList<T>where
T: RefUnwindSafe,
impl<T> Send for VList<T>where
T: Send,
impl<T> Sync for VList<T>where
T: Sync,
impl<T> Unpin for VList<T>
impl<T> UnwindSafe for VList<T>where
T: RefUnwindSafe + 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