rust-lang / nomicon

The Dark Arts of Advanced and Unsafe Rust Programming
https://doc.rust-lang.org/nomicon/
Apache License 2.0
1.75k stars 258 forks source link

Subtyping and Variance - Trait variance not covered #338

Open marioortizmanero opened 2 years ago

marioortizmanero commented 2 years ago

I ran into an issue with variance & subtyping, so I started to investigate about the topic. I read Rustnomicon's page about it, the reference, and watched Jon Gjengset's video, and I finally understood it, more or less. However, I still couldn't tell why my specific issue was occurring, because the examples covered in these sources were quite basic.

I only discovered why I was running into that issue once I stumbled upon some section in Rust's developer book. It mentions that "traits with associated types must be invariant with respect to all of their inputs", which is exactly my case. But to learn this, I had to look through a book that should only be used by those that contribute to the Rust compiler.

It would be really nice if it was included in the Rustnomicon (or maybe in the reference, or both). Furthermore, the explanation in Rust's developer book uses compiler terminology and implementation-specific stuff that I'm not familiar with. To this day, I only know that these traits are invariant, but not really why.

Thanks in advance!