← All Exercises

Rust Generic Traits

advancedrust~30min

Background

Generic traits with associated types provide flexible abstractions for container and algorithm interfaces.

Problem

Create a generic Stack trait with associated type Item, and implement it for Vec with push, pop, and peek operations.

Input

Elements to push onto the stack.

Output

A stack supporting push, pop, and peek operations.

Sample tests

Sample 1

Input: push(1), push(2), pop()
Expected: Some(2)

Your solution

Learning objective

Learn generic traits, associated types vs type parameters, and trait implementation for generic types.