One way to do this is by defining a new struct that holds a reference to the Vector3D
, and a counter, and to implement Iterator
on that new struct. You can get this struct by calling .iter()
on the Vector3D
:
fn main() {
let vec3 = Vector3D {
x: 3.0,
y: 7.0,
z: 22.0,
};
for a in vec3.iter() {
dbg!(a);
}
}
[src/main.rs:8] a = 3.0
[src/main.rs:8] a = 7.0
[src/main.rs:8] a = 22.0
See the implementation details here: Rust Playground
Personally, I'd prefer converting the Vector3D
into an array and iterating over the array like RustyYato suggested. It's simpler and less code to write.