捕获的变量无法逃脱 `FnMut` 闭包主体 `FnMut` 闭包只能在执行时访问其捕获的变量

问题描述 投票:0回答:1

我有这个代码。孩子们来自 Bevy 查询。

let array_of_elements = children.into_iter()
                        .filter_map(|&item| {
                            match element_children.get_mut(item) {
                                Ok(value) => Some(value),
                                Err(_) => None,
                            }
                        })
                        .collect();

我需要的是让所有子元素都成为某种向量/数组类型,可以将其发送到另一个函数。

这是孩子们的定义:

fn object_interactions(
  ...,
  mut element_children: Query<
    (Option<&mut Text>, Option<&mut Button>, Option<&Interaction>, Entity)
>,
  mut dialog_query: Query<(&mut Style, &mut Children), With<Dialog>>,
...) {
  for (mut style, children) in dialog_query.iter_mut() {

     // here's are all things running inluding this array_of_elements creation

  }

}

在某些(值)上我收到此错误:

error: captured variable cannot escape `FnMut` closure body
   --> src/main.rs:188:46
    |
143 |     mut element_children: Query<
    |     -------------------- variable defined here
...
186 |                         .filter_map(|&item| {
    |                                           - inferred to be a `FnMut` closure
187 |                             match element_children.get_mut(item) {
    |                                   ---------------- variable captured here
188 |                                 Ok(value) => Some(value),
    |                                              ^^^^^^^^^^^ returns a reference to a captured variable which escapes the closure body
    |
    = note: `FnMut` closures only have access to their captured variables while they are executing...
    = note: ...therefore, they cannot allow references to captured variables to escape
rust borrow-checker bevy
1个回答
0
投票

我找到了另一种解决方案,那里甚至不需要儿童特征,但这是一个不同的针对具体问题的答案。简而言之,我只是在这个函数中生成子实体,而不是在此之前创建它们。

© www.soinside.com 2019 - 2024. All rights reserved.