Struct Stack

Source
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>

Source

pub fn new() -> Stack<T>

Creates an empty VecDeque.

§Examples
use ripin::Stack;

let mut stack: Stack<i32> = Stack::new();
Source

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);
Source

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);
Source

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);
Source

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    }
Source

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T, U> TryIntoRef<U> for T
where U: TryFromRef<T>,

Source§

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>

Performs the conversion.