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;
}
}
);
}
①
Popular Pragprog topics

Hi Brian,
Looks like the api for tinydb has changed a little. Noticed while working on chapter 7 that the .purge() call to the db throws...
New

Hello! On page xix of the preface, it says there is a community forum "… for help if your’re stuck on one of the exercises in this b...
New

Hi,
build fails on:
bracket-lib = “~0.8.1”
when running on Mac Mini M1 Rust version 1.5.0:
Compiling winit v0.22.2
error[E0308]: mi...
New

Page 28: It implements io.ReaderAt on the store type.
Sorry if it’s a dumb question but was the io.ReaderAt supposed to be io.ReadAt?
...
New

When I run the coverage example to report on missing lines, I get:
pytest --cov=cards --report=term-missing ch7
ERROR: usage: pytest [op...
New

The markup used to display the uploaded image results in a Phoenix.LiveView.HTMLTokenizer.ParseError error.
lib/pento_web/live/product_l...
New

Hi,
I am getting an error I cannot figure out on my test.
I have what I think is the exact code from the book, other than I changed “us...
New

Modern front-end development for Rails, second edition - Struggling to get the first chapter to work
After running /bin/setup, the first error was: The foreman' command exists in these Ruby versions: That was easy to fix: gem install ...
New

@parrt
In the context of Chapter 4.3, the grammar Java.g4, meant to parse Java 6 compilation units, no longer passes ANTLR (currently 4....
New

@mfazio23
Android Studio will not accept anything I do when trying to use the Transformations class, as described on pp. 140-141. Googl...
New
Other popular topics

I’m thinking of buying a monitor that I can rotate to use as a vertical monitor?
Also, I want to know if someone is using it for program...
New

Do the test and post your score :nerd_face:
:keyboard:
If possible, please add info such as the keyboard you’re using, the layout (Qw...
New

Learn different ways of writing concurrent code in Elixir and increase your application's performance, without sacrificing scalabilit...
New

This is going to be a long an frequently posted thread.
While talking to a friend of mine who has taken data structure and algorithm cou...
New

We’ve talked about his book briefly here but it is quickly becoming obsolete - so he’s decided to create a series of 7 podcasts, the firs...
New

Rails 7 completely redefines what it means to produce fantastic user experiences and provides a way to achieve all the benefits of single...
New

Author Spotlight: Dmitry Zinoviev (@aqsaqal)
Today we’re putting our spotlight on Dmitry Zinoviev, author of Data Science Essentials ...
New

Author Spotlight: David Bryant Copeland (@davetron5000)
We’re so happy to bring you another Author Spotlight, a series where we sit d...
New

Large Language Models like ChatGPT say The Darnedest Things.
The Errors They MakeWhy We Need to Document Them, and What We Have Decided ...
New

zig/http.zig at 7cf2cbb33ef34c1d211135f56d30fe23b6cacd42 · ziglang/zig.
General-purpose programming language and toolchain for maintaini...
New
Latest in Pragprog
Latest (all)
Categories:
My Saved Portals
-
None saved yet
Popular Portals
- /elixir
- /opensuse
- /rust
- /kotlin
- /ruby
- /erlang
- /python
- /clojure
- /react
- /quarkus
- /go
- /vapor
- /v
- /react-native
- /wasm
- /security
- /django
- /nodejs
- /centos
- /haskell
- /rails
- /fable
- /gleam
- /swift
- /js
- /deno
- /assemblyscript
- /tailwind
- /laravel
- /symfony
- /phoenix
- /crystal
- /typescript
- /debian
- /adonisjs
- /julia
- /arch-linux
- /svelte
- /spring
- /c-plus-plus
- /flutter
- /preact
- /actix
- /java
- /angular
- /ocaml
- /zig
- /kubuntu
- /scala
- /zotonic
- /vim
- /rocky
- /lisp
- /html
- /keyboards
- /vuejs
- /nim
- /emacs
- /nerves
- /elm