pub struct Stack<T> { /* private fields */ }Expand description
A LIFO stack backed by a Vec.
Elements are pushed and popped from the top of the stack. All operations are O(1) amortized.
§Examples
use graph_collections::Stack;
let mut s: Stack<i32> = Stack::new();
s.push(1);
s.push(2);
assert_eq!(s.pop(), Some(2));
assert_eq!(s.peek(), Some(&1));
assert_eq!(s.len(), 1);
assert!(!s.is_empty());Implementations§
Source§impl<T> Stack<T>
impl<T> Stack<T>
Sourcepub fn new() -> Stack<T>
pub fn new() -> Stack<T>
Creates a new empty stack.
§Examples
use graph_collections::Stack;
let s: Stack<i32> = Stack::new();
assert!(s.is_empty());Sourcepub fn push(&mut self, value: T)
pub fn push(&mut self, value: T)
Pushes a value onto the top of the stack.
§Examples
use graph_collections::Stack;
let mut s: Stack<i32> = Stack::new();
s.push(10);
assert_eq!(s.peek(), Some(&10));Sourcepub fn pop(&mut self) -> Option<T>
pub fn pop(&mut self) -> Option<T>
Removes and returns the top value, or None if the stack is empty.
§Examples
use graph_collections::Stack;
let mut s: Stack<i32> = Stack::new();
s.push(1);
s.push(2);
assert_eq!(s.pop(), Some(2));
assert_eq!(s.pop(), Some(1));
assert_eq!(s.pop(), None);Sourcepub fn peek(&self) -> Option<&T>
pub fn peek(&self) -> Option<&T>
Returns a reference to the top value without removing it,
or None if the stack is empty.
§Examples
use graph_collections::Stack;
let mut s: Stack<i32> = Stack::new();
s.push(5);
assert_eq!(s.peek(), Some(&5));
assert_eq!(s.len(), 1); // peek does not removeTrait Implementations§
Source§impl<T> FromIterator<T> for Stack<T>
impl<T> FromIterator<T> for Stack<T>
Source§impl<T> IntoIterator for Stack<T>
impl<T> IntoIterator for Stack<T>
impl<T> StructuralPartialEq for Stack<T>
Auto Trait Implementations§
impl<T> Freeze for Stack<T>
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§
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