Traits with generics and Traits inheritance

can anybody give an simple example for Traits with generics and Traits inheritance

There’s no real trait inheritance in Rust. The syntax that looks like inheritance:

trait Foo: Bar {

means “things that implement Foo need to implement Bar as well”, but it doesn’t do much beyond just requiring two traits at the same time.

Traits with generics is whenever you have a generic argument, and require that argument to implement a trait:

// these are the same
fn foo<T: Foo>(arg: T) {}
fn foo<T>(arg: T) where T: Foo {}
// this is almost the same
fn foo(arg: impl T) {}

in all cases the function accepts one argument of any type, as long as there’s implementation of trait Foo for that type.

As kornel states:

trait Bar {
    fn frob(&self) -> bool;

trait Foo: Bar {
    fn bleep(&self) -> bool;

struct Zep {}

impl Bar for Zep {
    fn frob(&self) -> bool {

impl Foo for Zep {
    fn bleep(&self) {
        // defer to `Bar` trait implementation

Given the requirement that things implementing Foo must implement Bar, Foo implementations are allowed to use Bar functions, almost in the manner of superclass methods.

is multiple inheritance possible

It’s possible for a single type to implement multiple traits.

In general object-oriented programming patterns based around inheritance hierarchies are a poor fit for Rust. Inheritance-based design patterns require rethinking and approaching them as “flatter” problems of interfaces and polymorphism without inheritance.


It would be useful to collect summaries such as the above in a document, perhaps titled “A guide to Rust for O-O programmers”. Questions about inheritance in Rust keep surfacing, even though the Rust documentation states clearly that Rust does not support O-O inheritance per se.

1 Like

The book does have a chapter on this.

Please take a moment to use the forum search. You’ll find quite a bit more written on these subjects.

There are some replies in your previous topic with that same question
(Multilevel Inheritance). If those are not specific enough to your problem, let us know what other questions you have.