Below, are foo
and foo2
semantically identical?
fn foo<'a, T>(_: &'a T) {
}
fn foo2<'a, T: 'a>(_: &'a T) {
}
If so, how does the compiler know about the implicit lifetime bound in foo
; but it requires the explicit annotation of that lifetime bound in the implementation of std::iter::Iterator::partition_in_place
:
fn partition_in_place<'a, T: 'a, P>(mut self, ref mut predicate: P) -> usize
where
Self: Sized + DoubleEndedIterator<Item = &'a mut T>,
P: FnMut(&T) -> bool,
{
â‹®
}
To play it safe, it is my understanding that T: 'a
means that if the type T
happens to have internal references, then all of those references are guaranteed to last at least as long as 'a
. I don’t see how foo
is correct without such a guarantee since clearly in order for the parameter _
to be valid, it must be the case that the data it refers to lasts at least as long as _
itself. This is why I’m guessing that foo
and foo2
are equivalent, and that the compiler is smart enough to know this. I don’t understand why it wouldn’t be smart enough to know why Self: Sized + DoubleEndedIterator<Item = &'a mut T>
requires T
to have the lifetime bound T: 'a
and allow one to drop the explicit annotation.
Similarly, why doesn’t the implementation of std::iter::IntoIterator
for &'a std::vec::Vec<T>
require T
to have the lifetime bound T: 'a
since it assigns the associated type Item
to be &'a T
? Is it because the associated type IntoIter
is assigned std::slice::Iter
which in turn explicitly states T: 'a
allowing the compiler to infer this lifetime bound for &'a std::vec::Vec<T>
?