#Getting mutable and immutable components from the same query

7 messages · Page 1 of 1 (latest)

vast smelt
#

Hello. I have a function called in a system, with a query parameter defined like so:
pheromone_query: &mut Query<&mut Pheromone>

I'm using both get and get_mut for some operations, and obviously Rust started screaming at me. The error's the following:
| 214 | let mut local_pheromone = pheromone_query.get_mut(*local_pheromone).unwrap(); | ----------------------------------------- mutable borrow occurs here ... 244 | let adjacent_pheromone = pheromone_query.get(*adjacent_pheromone).unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ immutable borrow occurs here ... 260 | local_pheromone.intensity = max_pheromone_intensity; | --------------- mutable borrow later used here
Now, what I'm doing is getting a specific pheromone, analyzing the pheromones around it and updating the initial pheromone intensity. Since this isn't being done in parallel or anything, I don't understand why it's claiming the mutable borrow (for the query?) is being used at line 260, since i'm simply updating a field of the pheromone i already fetched mutably. Can anyone help me understand what's going on? I saw in the cheat book the chapter about ParamSets but I don't think my case requires it, i'm confused. Thanks in advance! (The unwrap is temporary, I swear :^) )

topaz briar
#

to get the immutable borrow, no mutable borrows can be held, so they get implicitly dropped

vast smelt
#

So if i have a mutable reference to a component, the query is also locked up and I can't get anything else from it unless the initial mutable reference is dropped

#

I just tried modifying my code to replace get_mut with get at line 214, and to get the actual mutable reference immediately before modifying my pheromone at line 260 and it isn't erroring anymore

vast smelt
topaz briar
#

i'm pretty sure that's right

vast smelt
#

thanks!