pub struct Stack<T>(/* private fields */);
Expand description
A growable stack implementing push/pop
actions.
use ripin::Stack;
let mut stack = Stack::new();
stack.push(3);
stack.push(4);
stack.push(10);
assert_eq!(stack.len(), 3);
assert_eq!(stack.pop(), Some(10));
assert_eq!(stack.len(), 2);
Implementations§
Source§impl<T> Stack<T>
impl<T> Stack<T>
Sourcepub fn with_capacity(capacity: usize) -> Stack<T>
pub fn with_capacity(capacity: usize) -> Stack<T>
Creates an empty VecDeque with space for at least n elements.
§Examples
use ripin::Stack;
let mut stack: Stack<i32> = Stack::with_capacity(10);
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements in the set.
§Examples
use ripin::Stack;
let mut stack = Stack::new();
stack.push(3);
assert_eq!(stack.len(), 1);
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the set contains no elements.
§Examples
use ripin::Stack;
let mut stack = Stack::new();
assert_eq!(stack.is_empty(), true);
stack.push(3);
assert_eq!(stack.is_empty(), false);
Sourcepub fn push(&mut self, value: T)
pub fn push(&mut self, value: T)
Appends an element to the back of the stack.
§Panics
Panics if the number of elements in the stack overflows a usize.
§Examples
use ripin::Stack;
let mut stack = Stack::new();
stack.push(3);
assert_eq!(stack.pop(), Some(3));
assert_eq!(stack.pop(), None);
Examples found in repository?
examples/custom_expression.rs (line 98)
92 fn evaluate(self, stack: &mut Stack<MyOperand>) -> Result<(), Self::Err> {
93 let (a, b) = pop_two_operands(stack).ok_or(MyEvalErr::NotEnoughOperands)?;
94 match self {
95 MyEvaluator::Add => {
96 match (a, b) {
97 (MyOperand::Number1, MyOperand::Number1) => {
98 Ok(stack.push(MyOperand::Number2))
99 },
100 _ => Err(MyEvalErr::CannotAddOperands(a, b)),
101 }
102 },
103 MyEvaluator::Sub => {
104 match (a, b) {
105 (MyOperand::Number2, MyOperand::Number1) => {
106 Ok(stack.push(MyOperand::Number1))
107 },
108 _ => Err(MyEvalErr::CannotSubOperands(a, b)),
109 }
110 }
111 _ => unreachable!() // _Phantom
112 }
113 }
Sourcepub fn pop(&mut self) -> Option<T>
pub fn pop(&mut self) -> Option<T>
Removes the last element from the stack and returns it,
or None
if it is empty.
§Examples
use ripin::Stack;
let mut stack = Stack::new();
stack.push(3);
stack.push(2);
assert_eq!(stack.pop(), Some(2));
assert_eq!(stack.pop(), Some(3));
assert_eq!(stack.pop(), None);
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
Source§impl<T, U> TryIntoRef<U> for Twhere
U: TryFromRef<T>,
impl<T, U> TryIntoRef<U> for Twhere
U: TryFromRef<T>,
Source§type Err = <U as TryFromRef<T>>::Err
type Err = <U as TryFromRef<T>>::Err
The type returned in the event of a conversion error.
Source§fn try_into_ref(&self) -> Result<U, <U as TryFromRef<T>>::Err>
fn try_into_ref(&self) -> Result<U, <U as TryFromRef<T>>::Err>
Performs the conversion.