How do I write the lifetimes for references in a type constraint when one of them is a local reference?

You need higher-ranked trait bounds (HRTBs), which are described in the advanced Rust book Rustonomicon and well as on Stack Overflow. They allow a type constraint to say that trait must be implemented not just for references with a particular lifetime but for any lifetime. They use the where for<> syntax. Here is the function … Read more

Why doesn’t the example compile, aka how does (co-, contra-, and in-) variance work?

Generically, a covariant type parameter is one which is allowed to vary down as the class is subtyped (alternatively, vary with subtyping, hence the “co-” prefix). More concretely: trait List[+A] List[Int] is a subtype of List[AnyVal] because Int is a subtype of AnyVal. This means that you may provide an instance of List[Int] when a … Read more