Skip to main content
In this lesson, we explore advanced traits in Rust. You’ll learn about associated types and how to implement traits for different types, deepening your understanding of Rust’s powerful type system.

Recap: Traits in Rust

Before exploring advanced topics, let’s review the basics of traits in Rust. A trait defines a set of methods that a type must implement to exhibit a specific behavior, serving as a contract or interface for the type. For example, consider the Display trait:
Any type that implements the Display trait must provide its own definition of the display method. For instance, here’s how you might implement this trait for a Person struct:
In this example, the Person struct implements the display method to print its name field.

Associated Types in Traits

An associated type acts as a placeholder within a trait definition. When a type implements the trait, it replaces this placeholder with a concrete type. Consider the following Container trait, which uses an associated type Item:
Here, the store method accepts an argument of type Self::Item, and the retrieve method returns a value of the same type. The actual type for Item is specified by the implementer of this trait.

Implementing the Container Trait for a Box

Let’s implement the Container trait for a simple Box struct that stores an integer value:
In this implementation, the associated type Item is set to i32. The store method accepts an integer and the retrieve method returns an integer. To see these methods in action:
The expected output will be:
Remember that specifying an associated type enables a more flexible and type-safe way of defining generic behaviors in Rust.

Implementing the Container Trait for a String Collection

Now, let’s create a struct that acts as a collection for strings and implement the Container trait for it. In this example, the container will exclusively manage string values:
Here, the associated type is explicitly set as String. The store method prints the stored string, and the retrieve method returns a clone of the first string in the items vector. To use the StringContainer:
The output will be:
This example demonstrates how associated types enable type-safe, flexible container implementations without ambiguity.
For more details on Rust traits and associated types, consider checking out the Rust Documentation.

Watch Video