rust_algorithm_club::collections

Struct 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§

Source§

impl<T> Stack<T>

Source

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

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.

Source

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.

Source

pub fn size(&self) -> usize

§Returns

Returns the size of collection, indicates how many items are added in the collection.

§Note

Size and capacity are different concepts. Capacity limits how many items can be stored, while size indicates how many items is currently stored.

Source

pub fn peek(&self) -> Option<&T>

Peeks the last element added without tampering the collection.

§Returns

Returns the most recently added item. If nothing was added, None will be returned.

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.