我如何迭代 Bevy 查询并保留对迭代值的引用以便我以后可以使用它?

How can I iterate on a Bevy Query and keep a reference to the iterated value so that I can use it later?

我在 empty 变量中有借用,我想延长它的寿命。在注释代码块中,我尝试解决它,但参考不再可用。我必须再次循环以找到匹配项才能对其进行操作。

如何循环查询以寻找最佳匹配,然后在知道它是最佳匹配后对其采取行动,而不必再次循环查找?

use bevy::prelude::*;

struct Person;
struct Name(String);

fn main() {
    App::build()
        .add_default_plugins()
        .add_startup_system(startup.system())
        .add_system(boot.system())
        .run();
}

fn boot(mut query: Query<(&Person, &mut Name)>) {
    let mut temp_str = String::new();
    let mut empty: Option<&mut Name> = None;
    for (_p, mut n_val) in &mut query.iter() {
        if n_val.0.to_lowercase() > temp_str.to_lowercase() {
            temp_str = n_val.0.clone();
            empty = Some(&mut n_val);
        }
    }
    println!("{}", temp_str);
    if let Some(n) = empty {
        // ...
    }
    // for (_p, mut n_val) in &mut query.iter() {
    //     if n_val.0 == temp_str {
    //         n_val.0 = "a".to_string();
    //     }
    // }
}

fn startup(mut commands: Commands) {
    commands
        .spawn((Person, Name("Gene".to_string())))
        .spawn((Person, Name("Candace".to_string())))
        .spawn((Person, Name("Zany".to_string())))
        .spawn((Person, Name("Sarah".to_string())))
        .spawn((Person, Name("Carl".to_string())))
        .spawn((Person, Name("Robert".to_string())));
}

Cargo.toml:

[package]
name = "sample"
version = "0.1.0"
authors = [""]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
bevy = "0.1.3"

具体错误:

error[E0716]: temporary value dropped while borrowed
  --> src/main.rs:17:33
   |
17 |     for (_p, mut n_val) in &mut query.iter() {
   |                                 ^^^^^^^^^^^-
   |                                 |          |
   |                                 |          temporary value is freed at the end of this statement
   |                                 creates a temporary which is freed while still in use
...
24 |     if let Some(n) = empty {
   |                      ----- borrow later used here
   |
   = note: consider using a `let` binding to create a longer lived value

error[E0597]: `n_val` does not live long enough
  --> src/main.rs:20:26
   |
20 |             empty = Some(&mut n_val);
   |                          ^^^^^^^^^^ borrowed value does not live long enough
21 |         }
22 |     }
   |     - `n_val` dropped here while still borrowed
23 |     println!("{}", temp_str);
24 |     if let Some(n) = empty {
   |                      ----- borrow later used here

,但这不是您的问题,错误显示 temporary value dropped while borrowed,因此您必须延长临时文件的使用寿命。

如果您想知道什么是临时的,编译器还会在错误消息中指出(字面意思):query.iter()。这是一个函数调用,returned 值没有绑定到任何东西,因此编译器为此创建了一个临时值。然后使用对该临时文件的引用进行迭代。当 for 循环结束时,临时文件将被丢弃,并且任何对它的引用生命周期都将到期。

解决方案是将临时变量绑定到局部变量。这样你就可以将对象的生命周期延长到变量的范围内:

let mut iter = query.iter();
for (_p, n_val) in &mut iter {
    if n_val.0.to_lowercase() > temp_str.to_lowercase() {
        temp_str = n_val.0.clone();
        empty = Some(n_val);
    }
}

PS:我发现遍历 &mut iter 的模式很奇怪。我希望 iter() 的 return 直接实现 IteratorIntoIterator,但它看起来像 this is not the case.