I need to pass an array of children to function. I tried it in two ways:
- By pushing children to previously created mutable array.
- with iterator
fn object_interactions(
mut dialog_query: Query<(&mut Style, &mut Children), With<Dialog>>,
mut element_children: Query<(Option<&mut Text>,Option<&mut Button>, Option<&Interaction>, Entity)>,
// ... more queries ...
) {
for contact_force_event in contact_force_events.iter() {
let ContactForceEvent { collider1, collider2, .. } = contact_force_event.clone();
for (player_entity, mut player, mut player_transform) in player_query.iter_mut() {
for (mut style, children) in dialog_query.iter_mut() {
for (object_entity, object) in object_query.iter() {
if
(collider1 == player_entity && collider2 == object_entity) ||
(collider1 == object_entity && collider2 == player_entity)
{
// THE IMPORTANT PART - first approach
let mut array_of_elements= vec![];
for &child in children.iter() {
let result_element = element_children.get_mut(child);
if let Ok(element) = result_element {
array_of_elements.push(element);
}
}
// THE IMPORTANT PART - first approach
// THE IMPORTANT PART - second approach
let array_of_elements = children.into_iter()
.filter_map(|&item| {
match element_children.get_mut(item) {
Ok(value) => Some(value),
Err(_) => None,
}
})
.collect();
// THE IMPORTANT PART - second approach
object.interact(
// ...different parameters
array_of_elements,
);
}
}
}
}
}
}
On the first approach I get:
cannot borrow `element_children` as mutable more than once at a time
`element_children` was mutably borrowed here in the previous iteration of the loop
on result_element. Ofcourse, if I don't push it to this array_of_elements array, there is no error(and actually just nothing, because I am sending empty array then)
On the second approach, I get an error in Some(value) part :
captured variable cannot escape `FnMut` closure body
`FnMut` closures only have access to their captured variables while they are executing...
...therefore, they cannot allow references to captured variables to escape
And also some "variable defined here" and "variable captured here" on "element_children" + "inferred to be a FnMut closure" on &item inside filter_map