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;
​ 	            }
​ 	        }
​ 	    );
​ 	}
​①​

Popular Prag Prog topics Top

mikecargal
Title: Hands-On Rust (Chap 8 (Adding a Heads Up Display) It looks like ​.with_simple_console_no_bg​(SCREEN_WIDTH*2, SCREEN_HEIGHT*2...
New
AleksandrKudashkin
On the page xv there is an instruction to run bin/setup from the main folder. I downloaded the source code today (12/03/21) and can’t see...
New
Chrichton
Dear Sophie. I tried to do the “Authorization” exercise and have two questions: When trying to plug in an email-service, I found the ...
New
swlaschin
The book has the same “Problem space/Solution space” diagram on page 18 as is on page 17. The correct Problem/Solution space diagrams ar...
New
patoncrispy
I’m new to Rust and am using this book to learn more as well as to feed my interest in game dev. I’ve just finished the flappy dragon exa...
New
rainforest
Hi, I’ve got a question about the implementation of PubSub when using a Phoenix.Socket.Transport behaviour rather than channels. Before ...
New
mert
AWDWR 7, page 152, page 153: Hello everyone, I’m a little bit lost on the hotwire part. I didn’t fully understand it. On page 152 @rub...
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
redconfetti
Docker-Machine became part of the Docker Toolbox, which was deprecated in 2020, long after Docker Desktop supported Docker Engine nativel...
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

Other popular topics Top

dasdom
No chair. I have a standing desk. This post was split into a dedicated thread from our thread about chairs :slight_smile:
New
Rainer
My first contact with Erlang was about 2 years ago when I used RabbitMQ, which is written in Erlang, for my job. This made me curious and...
New
AstonJ
This looks like a stunning keycap set :orange_heart: A LEGENDARY KEYBOARD LIVES ON When you bought an Apple Macintosh computer in the e...
New
DevotionGeo
The V Programming Language Simple language for building maintainable programs V is already mentioned couple of times in the forum, but I...
New
OvermindDL1
Woooooooo! This is such a huge release for it, and 2 years incoming! In short, the library is now using an updated hyper backend (not j...
New
AstonJ
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
PragmaticBookshelf
Author Spotlight Erin Dees @undees Welcome to our new author spotlight! We had the pleasure of chatting with Erin Dees, co-author of ...
New
New
DevotionGeo
I have always used antique keyboards like Cherry MX 1800 or Cherry MX 8100 and almost always have modified the switches in some way, like...
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

Latest in PragProg

View all threads ❯