Struct rust_algorithm_club::collections::Stack
source · [−]pub struct Stack<T> { /* private fields */ }
Expand description
A stack-like data structure implemented through a Vec
.
The name “stack” for this type of structure comes from the analogy to a set of physical items stacked on top of each other, which makes it easy to take an item off the top of the stack, while getting to an item deeper in the stack may require taking off multiple other items first.
Considered as a linear data structure, or more abstractly a sequential collection, the push and pop operations occur only at one end of the structure, referred to as the top of the stack.
References:
Implementations
sourceimpl<T> Stack<T>
impl<T> Stack<T>
sourcepub fn with_capacity(maxsize: usize) -> Self
pub fn with_capacity(maxsize: usize) -> Self
Initialize a stack of certain capacity.
Parameters
maxsize
: Capacity of the collection. It limits how many items can be stored.
sourcepub fn pop(&mut self) -> Option<T>
pub fn pop(&mut self) -> Option<T>
Removes the most recently added element that was not yet removed.
Returns
Returns the most recently added item. If nothing was added, None
will be returned.
Complexity
Constant.
sourcepub fn push(&mut self, item: T) -> bool
pub fn push(&mut self, item: T) -> bool
Adds an element to the collection.
Returns
Returns true
if the collection has space left and item is
successfully added, otherwise returns false
.
Complexity
Constant.
Auto Trait Implementations
impl<T> RefUnwindSafe for Stack<T> where
T: RefUnwindSafe,
impl<T> Send for Stack<T> where
T: Send,
impl<T> Sync for Stack<T> where
T: Sync,
impl<T> Unpin for Stack<T> where
T: Unpin,
impl<T> UnwindSafe for Stack<T> where
T: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more