How is the "outlives trick" wrong?
⚓ Rust 📅 2025-08-03 👤 surdeus 👁️ 11I have the following function which seems to be working for all the use cases I currently have.
pub fn filter_strs<'a, 'b: 'a>(
str_iter: impl IntoIterator<Item = &'a str> + 'b,
search_term: &'b str,
) -> impl Iterator<Item = String> + 'a + 'b {
str_iter
.into_iter()
.filter_map(move |s| s.contains(search_term).then_some(s.to_owned()))
}
I read the RFC for the new capture rules in the 2024 edition, and it says I’m using is “outlives trick”.
“But this is actually a non-solution in the general case.”
I see that I should be writing the signature like this instead:
pub fn filter_strs<'a, 'b, I>(
str_iter: I,
search_term: &'b str,
) -> impl Iterator<Item = String> + use<'a, 'b, I>
where
I: IntoIterator<Item = &'a str>;
However, I don’t actually understand the difference. The explanation in the RFC is too abstract for me to understand. I tried writing a concrete program that fails where it shouldn’t, playground link. However, all the examples I wrote still pass the borrow checker. Can someone show me an actual example where this fails and the new, more correct use<> syntax passes?
6 posts - 3 participants
🏷️ Rust_feed