Naive Iterator question

⚓ Rust    📅 2026-02-07    👤 surdeus    👁️ 1      

surdeus

Info

This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: Naive Iterator question

My apologies in advance if this is obvious and I simply missed it.
I have a number of cases in the toy program I am using to learn Rust where I have a struct (say S), with a field (say f), whose type is Vec where C is some content struct. (in one case, just a usize; in other cases somewhat more.) I want to have a method on S that returns an iterator over the field F. I tried what I thought was the obvious definition of returning self.f.iter(). And the compiler, presuambly quit ecorrectly, complained that I was trying to return an internal value.
Do I need to clone(or copy?) f before I invoke .iter()? I am quite happy if the iterator produces only immutable references to the innards.
Thanks,
Joel

4 posts - 2 participants

Read full topic

🏷️ Rust_feed