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

Lifetime of temporaries

A temporary object is destroyed when the full-expression that lexically contains the rvalue whose evaluation created that temporary object is completely evaluated. Let me demonstrate with ASCII art: ____________________ full-expression ranges from ‘b’ to last ‘)’ bar( foo().c_str() ); ^^^^^ ^ | | birth funeral

Why does linking lifetimes matter only with mutable references?

Warning: I’m speaking from a level of expertise that I don’t really have. Given the length of this post, I’m probably wrong a large number of times. TL;DR: Lifetimes of top-level values are covariant. Lifetimes of referenced values are invariant. Introducing the problem You can simplify your example significantly, by replacing VecRef<‘a> with &’a mut … Read more