Exercise 8 · Chapter: GenericsRequired+50 XP
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:
Top: Some(30)
Hints:
Stack::new()returnsStack { items: Vec::new() }pushusesself.items.push(item)peekusesself.items.last()
rust
fn main() {
}
Output
Run your code to see the output here.