Hands-on Rust: What does movers.iter_mut(ecs) do: Chapter 7

@herbert
Again using epub so don’t know the exact page number.

In this snipped of code, what does the movers.iter_mut(ecs) line do in the following code?
I think that movers is a list of tuples that we stored in the ECS and grabbed from the query.
I think that the iter_mut gives us a mutable iterator on the list of tuples.
What I don’t understand is why we pass in the ecs object to the iter_mut. I would think that it would be movers.iter_mut()

​ 	​use​ ​crate​::​prelude​::*;
​ 	
​ 	#[system]
​ 	#[write_component(Point)]
​ 	#[read_component(MovingRandomly)]
​​①​	​pub​ ​fn​ ​random_move​(ecs: &​mut​ SubWorld, ​#​[resource] map: &Map) {
​​②​	    ​let​ ​mut​ movers = <(&​mut​ Point, &MovingRandomly)>::​query​();
​ 	    movers
​ 	        ​.iter_mut​(ecs)
​ 	        ​.for_each​(|(pos, _)| {
​ 	            ​let​ ​mut​ rng = ​RandomNumberGenerator​::​new​();
​​③​	            ​let​ destination = ​match​ rng​.range​(0, 4) {
​ 	                0 ​=>​ ​Point​::​new​(​-​1, 0),
​ 	                1 ​=>​ ​Point​::​new​(1, 0),
​ 	                2 ​=>​ ​Point​::​new​(0, ​-​1),
​ 	                _ ​=>​ ​Point​::​new​(0, 1),
​ 	            } + *pos;
​ 	
​​④​	            ​if​ map​.can_enter_tile​(destination) {
​​⑤​	                *pos = destination;
​ 	            }
​ 	        }
​ 	    );
​ 	}
​①​

Sorry for the slow reply, I’ve been at home caring for a baby with daycare plague.

This is a quirk of how Legion works. When we build the query structure in-function, Legion has no way of knowing which world we mean. We’ve explicitly requested a SubWorld (with access to Point and MovingRandomly) - so we know that we need that world (the ecs variable), but the query structure itself doesn’t know that. So we have to tell it, as a parameter to iter_mut.

If I were revising it, I’d probably put the query in the system parameters (with automatic wiring up of the subworld). I’ll make a note for a potential future second edition.

Thanks!

Thanks that helps. I think we have other spots in the code where we use a more “traditional” iter() like I would expect.