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!

Where Next?

Popular Pragmatic Bookshelf topics Top

lirux
Hi Jamis, I think there’s an issue with a test on chapter 6. I own the ebook, version P1.0 Feb. 2019. This test doesn’t pass for me: ...
New
herminiotorres
Hi @Margaret , On page VII the book tells us the example and snippets will be all using Elixir version 1.11 But on page 3 almost the en...
New
raul
Hi Travis! Thank you for the cool book! :slight_smile: I made a list of issues and thought I could post them chapter by chapter. I’m rev...
New
cro
I am working on the “Your Turn” for chapter one and building out the restart button talked about on page 27. It recommends looking into ...
New
rmurray10127
Title: Intuitive Python: docker run… denied error (page 2) Attempted to run the docker command in both CLI and Powershell PS C:\Users\r...
New
jskubick
I found an issue in Chapter 7 regarding android:backgroundTint vs app:backgroundTint. How to replicate: load chapter-7 from zipfile i...
New
oaklandgit
Hi, I completed chapter 6 but am getting the following error when running: thread 'main' panicked at 'Failed to load texture: IoError(O...
New
AufHe
I’m a newbie to Rails 7 and have hit an issue with the bin/Dev script mentioned on pages 112-113. Iteration A1 - Seeing the list of prod...
New
bjnord
Hello @herbert ! Trying to get the very first “Hello, Bracket Terminal!" example to run (p. 53). I develop on an Amazon EC2 instance runn...
New
SlowburnAZ
Getting an error when installing the dependencies at the start of this chapter: could not compile dependency :exla, "mix compile" failed...
New

Other popular topics Top

Devtalk
Reading something? Working on something? Planning something? Changing jobs even!? If you’re up for sharing, please let us know what you’...
1023 17214 380
New
Exadra37
Please tell us what is your preferred monitor setup for programming(not gaming) and why you have chosen it. Does your monitor have eye p...
New
PragmaticBookshelf
Rust is an exciting new programming language combining the power of C with memory safety, fearless concurrency, and productivity boosters...
New
AstonJ
I have seen the keycaps I want - they are due for a group-buy this week but won’t be delivered until October next year!!! :rofl: The Ser...
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
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
rustkas
Intensively researching Erlang books and additional resources on it, I have found that the topic of using Regular Expressions is either c...
New
Help
I am trying to crate a game for the Nintendo switch, I wanted to use Java as I am comfortable with that programming language. Can you use...
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
New

Sub Categories: