Closed
Description
Generally speaking, '_
is equivalent to "eliding" a lifetime completely. That is, &'_ T
and &T
are equivalent, as are Foo<'_>
and Foo
(unfortunately). However, there are two cases where this is not true:
dyn Trait + '_
-- the object-lifetime-defaulting rules are different, and'_
needs to interact with them (same forTrait
, of course)impl Trait + '_
-- same, but for different reasons
@cramertj pointed out that our current behavior around dyn Trait
is not what we really want:
#![feature(dyn_trait)]
#![feature(underscore_lifetimes)]
// These two give the same error that the iterator must be valid for the 'static lifetime:
fn a<T>(items: &[T]) -> Box<dyn Iterator<Item=&T>> {
Box::new(items.iter()) //~ ERROR cannot infer an appropriate lifetime
}
fn b<T>(items: &[T]) -> Box<dyn Iterator<Item=&T> + '_> {
Box::new(items.iter()) // *Should* be OK, currently errors
}
// But this one works:
fn c<'a, T>(items: &'a [T]) -> Box<dyn Iterator<Item=&'a T> + 'a> {
Box::new(items.iter()) // OK
}
fn main() { }