6
votes

When defining a trait, my understanding is that trait names on the right side of the : are required any time the left side is implemented. If so, why does the following compile:

use std::any::Any;

trait Trait: Any {}

struct Thing {}

impl Trait for Thing {}

The following does not compile (which matches my understanding of what is correct)

trait RequiredTrait {}
trait Trait: RequiredTrait {}

struct Thing {}

impl Trait for Thing {}
1

1 Answers

7
votes

std::any contains the implementation:

impl<T> Any for T
where
    T: 'static + ?Sized, 

That means that any type implements Any as long as any references it contains are 'static and the type is sized. Your Thing struct meets both of those requirements so it does implement Any and your code compiles.