我正在尝试为游戏中的实体系统编写一些代码,但是我收到了这个错误所以我把代码煮成了同样的东西,我在实际代码中得到了同样的错误.
我不明白为什么编译器告诉我self.my_list在函数baz完成时引用超出范围.
我的锈版是 rustc 1.3.0 (9a92aaf19 2015-09-15)
for循环结束时,我会认为它超出了范围?
struct Foo {
name : &'static str,
}
struct Bar {
my_list : Vec<Foo>,
}
impl Bar {
fn New() -> Bar {
let mut new_instance = Bar { my_list : vec!() };
new_instance.my_list.push(Foo { name : "foo1" });
new_instance.my_list.push(Foo { name : "foo2" });
new_instance.my_list.push(Foo { name : "foo3" });
return new_instance;
}
fn Baz(&mut self, name : &'static str) -> Option<&Foo> {
for x in &self.my_list {
if x.name == name {
return Some(x);
}
}
self.my_list.push(Foo { name : "foo" });
return None;
}
}
fn main() {
let mut bar = Bar::New();
if let Some(x) = bar.Baz("foo1") {
println!("{} found", x.name);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误消息:
Compiling tutorial v0.1.0 (file:///C:/Code/Projects/rust/tutorial)
src\main.rs:35:9: 35:21 error: cannot borrow `self.my_list` as mutable because it is also borrowed as immutable
src\main.rs:35 self.my_list.push(Foo { name : "foo" });
^~~~~~~~~~~~
src\main.rs:29:19: 29:31 note: previous borrow of `self.my_list` occurs here; the immutable borrow prevents subsequent moves or mutable borrows of `self.my_list` until the borrow ends
src\main.rs:29 for x in &self.my_list {
^~~~~~~~~~~~
note: in expansion of for loop expansion
src\main.rs:29:9: 33:10 note: expansion site
src\main.rs:38:6: 38:6 note: previous borrow ends here
src\main.rs:28 fn Baz(&mut self, name : &'static str) -> Option<&Foo> {
...
src\main.rs:38 }
^
error: aborting due to previous error
Could not compile `tutorial`.
To learn more, run the command again with --verbose.
Run Code Online (Sandbox Code Playgroud)
这是借用检查器的限制。您推断该函数是安全的方式本质上是将函数视为两条路径:沿着函数返回的路径Some(x),您不会my_list再次借用,并且沿着函数返回的路径None,借用在 for 之后结束环形。
这不是借用检查器看待函数的方式;借用检查器根据词法范围进行工作。它尝试选择尽可能窄的范围,但如果需要,它会将整个函数视为范围。它看到返回值中的生命周期与 的生命周期相同self,因此 引用的值的生命周期x必须与 相同self,因此借用&self.my_list必须与 具有相同的生命周期self,因此借用的持续时间超出了返回的时间功能。
如果你以不同的方式编写函数,Rust 会接受它:
fn Baz(&mut self, name : &'static str) -> Option<&Foo> {
match self.my_list.iter().position(|x| x.name == name) {
Some(i) => Some(&self.my_list[i]),
None => {
self.my_list.push(Foo { name : "foo" });
None
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
146 次 |
| 最近记录: |