Struct trait_based_collection::stack::Stack
source · [−]pub struct Stack<T> { /* private fields */ }
Expand description
A stack is a collection that follows the LIFO (last-in-first-out) principle. This means that elements are added to the top of the stack and removed from the top of the stack. This implementation uses a linked-based structure.
Examples
use trait_based_collection::{prelude::*, Stack};
let mut queue: Stack<u32> = Stack::new_default();
queue.add(1);
queue.add(2);
queue.add(3);
assert_eq!(queue.peek(), Some(&3));
assert_eq!(queue.remove(), Some(3));
assert_eq!(queue.remove(), Some(2));
assert_eq!(queue.remove(), Some(1));
assert_eq!(queue.remove(), None);
Trait Implementations
sourceimpl<'a, T: 'a> Collection<'a, T> for Stack<T>
impl<'a, T: 'a> Collection<'a, T> for Stack<T>
sourcefn new_default() -> Self
fn new_default() -> Self
Creates a new
Collection
with a default capacity. Read moresourcefn add(&mut self, value: T)
fn add(&mut self, value: T)
Adds an item to the
Collection
. Read moresourcefn remove(&mut self) -> Option<T>
fn remove(&mut self) -> Option<T>
Removes an item from the
Collection
. Read moresourcefn peek(&'a self) -> Option<Self::ItemRef>
fn peek(&'a self) -> Option<Self::ItemRef>
Returns an immutable reference of the item that will be removed next. Read more
sourcefn peek_mut(&'a mut self) -> Option<Self::ItemMut>
fn peek_mut(&'a mut self) -> Option<Self::ItemMut>
Returns a mutable reference of the item that will be removed next. Read more
sourcefn get(&'a self, index: usize) -> Option<Self::ItemRef>
fn get(&'a self, index: usize) -> Option<Self::ItemRef>
Returns a immutable reference to the n-th item in the
Collection
. Read moresourcefn get_mut(&'a mut self, index: usize) -> Option<Self::ItemMut>
fn get_mut(&'a mut self, index: usize) -> Option<Self::ItemMut>
Returns a mutable reference to the n-th item in the
Collection
. Read moresourcefn len(&self) -> usize
fn len(&self) -> usize
Returns the number of items in the
Collection
. Read moresourcefn with_capacity(capacity: usize) -> Selfwhere
Self: Sized,
fn with_capacity(capacity: usize) -> Selfwhere
Self: Sized,
Creates a new
Collection
with a specific capacity. Read moresourcefn with_approximate_capacity(approx_capacity: usize) -> Selfwhere
Self: Sized,
fn with_approximate_capacity(approx_capacity: usize) -> Selfwhere
Self: Sized,
Creates a new
Collection
with a capacity that is at least the specified capacity. Read moresourcefn clear(&mut self)
fn clear(&mut self)
Clears all items from the
Collection
while keeping the capacity. Read moresourcefn is_empty(&self) -> bool
fn is_empty(&self) -> bool
Checks if the
Collection
is empty. Read moresourceimpl<T> Extend<T> for Stack<T>
impl<T> Extend<T> for Stack<T>
sourcefn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
Extends a collection with the contents of an iterator. Read more
sourcefn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
🔬This is a nightly-only experimental API. (
extend_one
)Extends a collection with exactly one element.
sourcefn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
🔬This is a nightly-only experimental API. (
extend_one
)Reserves capacity in a collection for the given number of additional elements. Read more
sourceimpl<T> FromIterator<T> for Stack<T>
impl<T> FromIterator<T> for Stack<T>
sourcefn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
Creates a value from an iterator. Read more
sourceimpl<'a, T> IntoIterator for &'a Stack<T>
impl<'a, T> IntoIterator for &'a Stack<T>
sourceimpl<'a, T> IntoIterator for &'a mut Stack<T>
impl<'a, T> IntoIterator for &'a mut Stack<T>
sourceimpl<T> IntoIterator for Stack<T>
impl<T> IntoIterator for Stack<T>
sourceimpl<'a, T: 'a> Iterators<'a, T> for Stack<T>
impl<'a, T: 'a> Iterators<'a, T> for Stack<T>
type ItemRef = &'a T
type ItemRef = &'a T
The type of reference the immutable iterator (
Iter
) iterates over the items in the
Collection
. The reference is only valid for the duration of the iteration. Read moretype ItemMut = &'a mut T
type ItemMut = &'a mut T
The type of mutable reference the mutable iterator (
IterMut
) iterates over the items in
the Collection
. The reference is only valid for the duration of the iteration. Read moretype IterMut = StackIterMut<'a, T>
type IterMut = StackIterMut<'a, T>
sourcefn iter(&'a self) -> Self::Iter
fn iter(&'a self) -> Self::Iter
Creates an immutable iterator over the items in the
Collection
without consuming them. Read moresourcefn iter_mut(&'a mut self) -> Self::IterMut
fn iter_mut(&'a mut self) -> Self::IterMut
Creates a mutable iterator over the items in the
Collection
without consuming them. Read moreAuto Trait Implementations
impl<T> RefUnwindSafe for Stack<T>where
T: RefUnwindSafe,
impl<T> !Send for Stack<T>
impl<T> !Sync for Stack<T>
impl<T> Unpin for Stack<T>
impl<T> UnwindSafe for Stack<T>where
T: RefUnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more