Reusable iterators over string references

⚓ Rust    📅 2025-05-01    👤 surdeus    👁️ 5      

surdeus

Warning

This post was published 112 days ago. The information described in this article may have changed.

I have a function that needs to take in a list of keys (strings), which need to be iterated over multiple times.

This works:

fn decode_list<I, K>(it: I)
where
  I: IntoIterator<Item = K> + Clone,
  K: AsRef<str>
{
  for idx in 0..2 {
    for n in it.clone() {
      let nm = n.as_ref();
    }
  }
}

fn main() {
  decode_list(["Id", "Name", "Age"]);
}

But I'm curious if it's possible to eliminate the need for the Clone? There are a number of posts about this, using HRTB, on these forums, but my attempts at adapting them cause the call to decode_list() to no longer be accepted.

Edit: Fixed the Pennywise Freudian slip

2 posts - 2 participants

Read full topic

🏷️ rust_feed