Parameterized Structs

When we looked at the Vec struct, we learned about type parameters. Those are the things that go inside the angle brackets, e.g., Vec<i32> has a type parameter of i32. Type parameters are very common in Rust, and can show up in many different parts of your code. They allow you to generalize structs, functions, and more from working on one single type to an entire family of types. Vec was just the tip of the iceberg. Let’s demonstrate how and why they are used.

Take our trusty old Fruit struct as an example:

Get hands-on with 1200+ tech skills courses.