Why does this borrow outlive its own scope – only when declared as a trait object?

trait lifetimes are invariant, I think you run into the "borrow self forever" problem:

try remove the explicit lifetime annotation on &self:

trait ReturnIter<'this> {
	// This dyn Iterator<…> needs `+ 'this` specified, lest the compiler require
	// it to be 'static. The issue happens with *any* return type (even a non-
	// `dyn` one), but this illustrates why the lifetime bound is needed.
	fn return_iter(&self) -> Box<dyn Iterator<Item = usize> + '_>;
}
2 Likes