
Chocrates
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;
}
}
);
}
①
First Post!

herbert
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!
Popular Pragmatic topics










Other popular topics









Latest in PragProg
Latest (all)
Categories:
Popular Portals
- /elixir
- /rust
- /wasm
- /ruby
- /erlang
- /phoenix
- /keyboards
- /js
- /rails
- /python
- /security
- /go
- /swift
- /vim
- /clojure
- /java
- /haskell
- /emacs
- /svelte
- /onivim
- /typescript
- /crystal
- /c-plus-plus
- /tailwind
- /kotlin
- /gleam
- /react
- /flutter
- /elm
- /ocaml
- /vscode
- /opensuse
- /ash
- /centos
- /php
- /deepseek
- /scala
- /zig
- /html
- /debian
- /nixos
- /lisp
- /agda
- /react-native
- /textmate
- /sublime-text
- /kubuntu
- /arch-linux
- /revery
- /ubuntu
- /manjaro
- /spring
- /django
- /diversity
- /nodejs
- /lua
- /slackware
- /julia
- /c
- /neovim