To &query or not the &query?
Hey! I'm very new to Rust and Bevy, so following along with the Bevy quickstart and taking time to learn when I see something new. I'm at the step where I'm making one of the first systems:
fn greet_people(query: Query<&Name, With<Person>>) {
for name in &query {
println!("hello {}!", name.0);
}
}
Here I took the time to learn a bit about &, how &Name is a reference to the Name component, and &query borrows it. I did a bit of web searching to learn, and I learned that if I didn't use the & on an iterator like that, it would be consumed, so I tried it out:
fn greet_people(query: Query<&Name, With<Person>>) {
for name in query {
println!("hello {}!", name.0);
}
for name in query {
println!("hello {}!", name.0);
}
}
To my surprise this does still work... But my understanding is that this is a special case to Bevy. Because for example if I did something similar with a vector, it does not compile:
let vec = vec![1, 2, 3];
for x in vec {} // moves vec
for x in vec {} // error: value used after move
The compiler tells me in the second use of vec it has been moved.
So I have a few questions:
- Can you confirm, this is a special case in Bevy?
- What does Bevy do exactly? Does it return a copy or something?
- Are there any reasons to use
for name in queryoverfor name in &queryor visa versa?