pub struct Stack<T> { /* private fields */ }
Implementations§
Source§impl<T> Stack<T>
impl<T> Stack<T>
Sourcepub fn push(&mut self, val: T)
pub fn push(&mut self, val: T)
Adds an item to the top of a stack.
§Examples
use simple_stack::Stack;
let mut stack = Stack::new();
stack.push(3);
assert_eq!(stack.peek(), Some(&3));
Sourcepub fn pop(&mut self) -> Option<T>
pub fn pop(&mut self) -> Option<T>
Removes the top item from a stack and returns it, or None
if the stack is empty.
§Examples
use simple_stack::Stack;
let mut stack = Stack::new();
stack.push(3);
stack.push(7);
assert_eq!(stack.pop(), Some(7));
assert_eq!(stack.pop(), Some(3));
assert_eq!(stack.pop(), None);
Sourcepub fn peek(&self) -> Option<&T>
pub fn peek(&self) -> Option<&T>
Returns a reference to the top item of a stack, or None
if
the stack is empty.
§Examples
use simple_stack::Stack;
let mut stack = Stack::new();
assert_eq!(stack.peek(), None);
stack.push(4);
assert_eq!(stack.peek(), Some(&4));
Sourcepub fn peek_mut(&mut self) -> Option<&mut T>
pub fn peek_mut(&mut self) -> Option<&mut T>
Returns a mutable reference to the top item of a stack, or None
if
the stack is empty.
§Examples
use simple_stack::Stack;
let mut stack = Stack::new();
stack.push(11);
assert_eq!(stack.peek_mut(), Some(&mut 11));
*stack.peek_mut().unwrap() += 1;
assert_eq!(stack.peek_mut(), Some(&mut 12));
Trait Implementations§
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>
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