LendingIterator<'a> with automatic .map, .filter, .for_each implementations

pub trait LendingIterator {
	type Item<'a>
	where
		Self: 'a;

	fn next<'a>(&'a mut self) -> Option<Self::Item<'a>>;

	fn for_each<'a, F>(
		mut self,
		mut f: F,
	) where
		Self: Sized + 'a,
		F: FnMut(Self::Item<'a>),
	{
		while let Some(x) = self.next() {
			f(x);
		}
	}
}

makes it possible for me to have an iterator over something with a lifetime on the next call. However, I end up having to implement everything like for_each, map, filter, etc.

Is there a way to automatically get implementations of these stuff for my LendingIterator?

There's already a maintained, modern lending iterator crate.

1 Like

will it land on the std lib?

Hmm… Used in two crates… 203 downloads last month… Answer is obvious, isn't it?

Except when there are extremely good reasons to add something to std without it being “proven in the wild” (e.g. when special support on the compiler side is needed and said thing can not be made into third-party craze) Rust usually waits for a long time before adding things to the std.

I guess support in for loop maybe one such thing which may prompt eventual addition of Landing iterator to the language… but I don't think it would happen any time soon…

No idea. But does it matter? If you don't want to implement the whole thing yourself, then it certainly does the job.

is a bit more popular.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.