Confusion about impls without for


#1

I was sure you can write an impl without for only for a struct/enum. The same is said in the reference:

It is possible to define an implementation without referring to a trait. The methods in such an implementation can only be used as direct calls on the values of the type that the implementation targets. In such an implementation, the trait type and for after impl are omitted. Such implementations are limited to nominal types (enums, structs), and the implementation must appear in the same crate as the self type.

But today I found that the following thing compiles and runs without a warning:

trait T {

}

impl T {
    fn foo() {
        println!("Hello, World!");
    }
}

fn main() {
    T::foo();
}

Is this a bug in the compiler, a bug in the reference, or a bug in my understanding of the laguage? :slight_smile:


What does impl trait mean?
How to write iterator adapter
#2

#3

Thanks!