Exerpad
🔥0
0 XP
Lv.1 Beginner
Log In

Generic Stack

Create a generic struct Stack<T> with a field items: Vec<T>. Add three methods: new() that creates an empty stack, push(&mut self, item: T) that adds an item, and peek(&self) -> Option<&T> that returns the last item. Test it with integers.

Expected output:

text
Top: Some(30)

Hints:

  • Stack::new() returns Stack { items: Vec::new() }
  • push uses self.items.push(item)
  • peek uses self.items.last()
rust
fn main() {
}