问题描述
我在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
解决方法
You cannot extend the lifetime of a reference,但这不是您要解决的问题,错误提示为temporary value dropped while borrowed
,因此您必须延长临时文件的寿命。
如果您想知道什么是临时的,编译器还会在错误消息query.iter()
中指向(从字面上看)。这是一个函数调用,返回的值未绑定任何内容,因此编译器为此创建了一个临时值。然后,使用对该临时目录的引用进行迭代。 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()
可以直接实现Iterator
或IntoIterator
,但看起来像this is not the case。