Generics
Generics in Rust allow you to write code that works with different types without duplicating code for each specific type. They are commonly used in functions, structs, enums, and methods to make your code more reusable and flexible.
For this challenge, you will implement a generic struct ItemContainer that can store any type of item and provide methods to retrieve and manipulate the item.
Your Task
- Define a generic struct
ItemContainer<T>with a single fielditem: T. - Implement a method
get_itemthat returns a reference to the item.
Hints
If you're stuck, here are some hints to help you solve the challenge:
Click here to reveal hints
-
Define the struct as
ItemContainer<T>. e.g.rust struct ItemContainer<T> { item: T, } -
Implement the method in the
impl<T>block. The method should take&selfand return&T.
No run yet
Press
Run to execute your code and see test results here.