Stack

Struct Stack 

Source
pub struct Stack<T> { /* private fields */ }

Implementations§

Source§

impl<T> Stack<T>

Source

pub fn new() -> Self

Source

pub fn push(&mut self, elem: T)

Source

pub fn pop(&mut self) -> Result<T, &str>

In pop function, we trying to:

  • check if the list is empty, so we use enum Option, it can either be Some(T) or None
    • if it’s empty, return None
    • if it’s not empty
      • remove the head of the list
      • remove its elem
      • replace the list’s head with its next
      • return Some(elem), as the situation if need

so, we need to remove the head, and return the value of the head

Source

pub fn is_empty(&self) -> bool

Source

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

Source

pub fn peek_mut(&mut self) -> Option<&mut T>

Source

pub fn into_iter_for_stack(self) -> IntoIter<T>

Source

pub fn iter(&self) -> Iter<'_, T>

Source

pub fn iter_mut(&mut self) -> IterMut<'_, T>

Trait Implementations§

Source§

impl<T> Default for Stack<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T> Drop for Stack<T>

The drop method of singly linked list. There’s a question that do we need to worry about cleaning up our list? As we all know the ownership and borrow mechanism, so we know the type will clean automatically after it goes out the scope, this implement by the Rust compiler automatically did which mean add trait drop for the automatically.

So, the complier will implements Drop for List->Link->Box<Node> ->Node automatically and tail recursive to clean the elements one by one. And we know the recursive will stop at Box https://rust-unofficial.github.io/too-many-lists/first-drop.html

As we know we can’t drop the contents of the Box after deallocating, so we need to manually write the iterative drop

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

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