For brainstorming purposes, I decided to try rewriting the suggested error message in the most explicit, non-technical language I could manage. I’m not sure how to reduce this to something we’d actually use, but maybe it can give someone ideas:
error: we need to infer a lifetime to use, but a lifetime here has requirements
that contradict each other
--> underscore-lifetime/dyn-trait-underscore.rs:18:5
|
16 | fn a<T>(items: &[T]) -> Box<dyn Iterator<Item=&T>> {
| ----------- -------------------------- when you use `dyn Trait` in this
context, it's implicitly `dyn Trait + 'static`, so this type is equivalent to
`Box<dyn Iterator<Item = &T> + 'static>`
| |
| when you use & without an explicit lifetime, it still has a lifetime,
even though that lifetime isn't named. Let's call this lifetime `'1`.
So we can write the type of `items` as `&'1 [T]`.
17 | Box::new(items.iter())
| ^^^^^^^^^^^^^^^^^^^^^^ casting a `Box<std::slice::Iter<'1, T>>` value
to `Box<dyn Iterator<Item = &T> + 'static>` is only possible if
`std::slice::Iter<'1, T>` obeys the trait bounds `Iterator<Item = &T> + 'static`.
And that only happens if '1 includes 'static`.
But the function signature allows '1 to be smaller than 'static.