pub struct Stack<T> { /* private fields */ }
Expand description
A stack data structure.
This structure implements a simple stack where elements of type T
are pushed and popped
according to the Last-In-First-Out (LIFO) principle. The stack has a fixed size of 100 elements.
Implementations§
Source§impl<T> Stack<T>
impl<T> Stack<T>
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new, empty Stack
.
§Examples
use dsa::data_structures::stack::Stack;
let stack: Stack<i32> = Stack::new();
assert!(stack.is_empty());
Sourcepub fn push(&mut self, value: T) -> Result<(), &str>
pub fn push(&mut self, value: T) -> Result<(), &str>
Pushes a value onto the Stack
.
§Examples
use dsa::data_structures::stack::Stack;
let mut stack = Stack::new();
stack.push(10).unwrap();
stack.push(20).unwrap();
assert_eq!(stack.peek(), Some(&20));
assert_eq!(stack.size(), 2);
§Parameters
value
: AT
generic value that will be pushed onto the top of thisStack
§Returns
- A
Result
object that return anErr
if thisStack
is full, otherwise an emptyOk(())
Sourcepub fn pop(&mut self) -> Option<T>
pub fn pop(&mut self) -> Option<T>
Pops a value from the Stack
.
§Examples
use dsa::data_structures::stack::Stack;
let mut stack: Stack<i32> = Stack::new();
stack.push(10).unwrap();
stack.push(20).unwrap();
stack.pop();
assert_eq!(stack.pop(), Some(10));
§Returns
- An optional
T
generic that represents the topmost value that has been removed from thisStack
Sourcepub fn peek(&self) -> Option<&T>
pub fn peek(&self) -> Option<&T>
Returns a reference to the top element without removing it.
§Examples
use dsa::data_structures::stack::Stack;
let mut stack: Stack<i32> = Stack::new();
stack.push(10).unwrap();
stack.push(20).unwrap();
stack.pop();
assert_eq!(stack.peek(), Some(&10));
§Returns
- The topmost value of this
Stack
as an optionalT
generic reference
Auto Trait Implementations§
impl<T> Freeze for Stack<T>where
T: Freeze,
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