Chocrates

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

herbert

Author of Hands-on Rust

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 Bookshelf topics Top

jimmykiang
This test is broken right out of the box… — FAIL: TestAgent (7.82s) agent_test.go:77: Error Trace: agent_test.go:77 agent_test.go:...
New
jimschubert
In Chapter 3, the source for index introduces Config on page 31, followed by more code including tests; Config isn’t introduced until pag...
New
kuroneko
Whilst the author has been careful to provide exact results for the tests elsewhere in the book (such as surds with the transformation te...
New
belgoros
Following the steps described in Chapter 6 of the book, I’m stuck with running the migration as described on page 84: bundle exec sequel...
New
raul
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
taguniversalmachine
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
davetron5000
Hello faithful readers! If you have tried to follow along in the book, you are asked to start up the dev environment via dx/build and ar...
New
SlowburnAZ
Getting an error when installing the dependencies at the start of this chapter: could not compile dependency :exla, "mix compile" failed...
New
dachristenson
I just bought this book to learn about Android development, and I’m already running into a major issue in Ch. 1, p. 20: “Update activity...
New
dachristenson
I’ve got to the end of Ch. 11, and the app runs, with all tabs displaying what they should – at first. After switching around between St...
New

Other popular topics Top

New
AstonJ
We have a thread about the keyboards we have, but what about nice keyboards we come across that we want? If you have seen any that look n...
New
AstonJ
Thanks to @foxtrottwist’s and @Tomas’s posts in this thread: Poll: Which code editor do you use? I bought Onivim! :nerd_face: https://on...
New
dimitarvp
Small essay with thoughts on macOS vs. Linux: I know @Exadra37 is just waiting around the corner to scream at me “I TOLD YOU SO!!!” but I...
New
Exadra37
I am asking for any distro that only has the bare-bones to be able to get a shell in the server and then just install the packages as we ...
New
AstonJ
Seems like a lot of people caught it - just wondered whether any of you did? As far as I know I didn’t, but it wouldn’t surprise me if I...
New
mafinar
Crystal recently reached version 1. I had been following it for awhile but never got to really learn it. Most languages I picked up out o...
New
foxtrottwist
A few weeks ago I started using Warp a terminal written in rust. Though in it’s current state of development there are a few caveats (tab...
New
First poster: bot
The overengineered Solution to my Pigeon Problem. TL;DR: I built a wifi-equipped water gun to shoot the pigeons on my balcony, controlle...
New
PragmaticBookshelf
Author Spotlight: Bruce Tate @redrapids Programming languages always emerge out of need, and if that’s not always true, they’re defin...
New

Sub Categories: