rust - 对struct方法中的借用错误感到困惑

标签 rust

我正在尝试为游戏中的实体系统编写一些代码,但出现此错误,因此我将代码简化为执行相同操作的代码,但我在实际代码中遇到了相同的错误。

我不明白为什么编译器告诉我当函数 baz 完成时对 self.my_list 的引用超出范围。

我的 Rust 版本是 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);
    }
}

这是我收到的错误消息:

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.

最佳答案

这是借用检查器的一个限制。您推断函数安全的方式本质上是将函数视为两条路径:沿着函数返回 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
        }
    }
}

关于rust - 对struct方法中的借用错误感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32882321/

相关文章:

rust - 如何更新HashMap中的 key ?

c - Rust 析构函数和所有权

rust - `use path::{self}` 是什么意思?

performance - 我可以改善 Rust zip crate 的糟糕性能吗?

rust - 子类型和方差(&mut 不变)违反 Rust 中的错误

android - 在Android上构建Rust

rust - 找不到目标 thumbv7em-none-eabihf 的 sin()、cos()、log10()( float )

rust - 无法返回向量切片 - ops::Range<i32> 未实现

rust - 如何在自定义结构序列化程序中嵌套字段?

string - 如何将字符串传递给 HashSet 包含?