ralfbiedert / cheats.rs

Rust Language Cheat Sheet - https://cheats.rs
https://cheats.rs
4.09k stars 382 forks source link

"Implementing Families — impl<>" compilation error #175

Closed scturtle closed 1 year ago

scturtle commented 1 year ago

The code in Working with Types -> Types, Traits, Generics -> Generics -> Implementing Families — impl<>:

use core::fmt::Display;

trait S<T> {
    fn foo(&self, x: T);
}

impl<T> S<T> where T: Copy + Display {
    fn foo(&self, x: T) {}
}

fn main() {}

causing a compilation error:

error[E0782]: trait objects must include the `dyn` keyword
 --> src/main.rs:7:9
  |
7 | impl<T> S<T> where T: Copy + Display {
  |         ^^^^
  |
help: add `dyn` keyword before this trait
  |
7 | impl<T> dyn S<T> where T: Copy + Display {
  |         +++

For more information about this error, try `rustc --explain E0782`.

It seems that a dyn should be added. Any more references about this feature?

ralfbiedert commented 1 year ago

Sorry for the confusion, S<T> is meant to be a type, not a trait. This works:

use core::fmt::Display;

struct S<T>(T);

impl<T> S<T> where T: Copy + Display {
    fn foo(&self, x: T) {}
}

fn main() {}
ralfbiedert commented 1 year ago

About your question, I've also added a link in that section to Generic Implementations.