借用检查器检查可变引用的最佳实践是什么?

What is the best practice with the borrow checker for inspecting mutable references?

我想获取一个链表并用结构的实例填充它,但前提是该列表尚未包含我正在考虑添加的项。

我正在处理点,所以如果 (3,5) 在列表中,我不想添加它,否则我会添加。

我当前的代码:

use std::collections::LinkedList;

struct Location {
    x: i32,
    y: i32,
}

fn main() {
    let mut locations = LinkedList::new();

    loop {
        let location_set = &mut locations;
        // Scanner stuff happens.
        if !has_location(location_set, &next_checkpoint_x, &next_checkpoint_y) {
            let point = Location {
                x: next_checkpoint_x,
                y: next_checkpoint_y,
            };
            locations.push_back(point);
        }
    }
}

fn has_location(location_list: LinkedList<Location>, target_x: &i32, target_y: &i32) -> bool {
    true // Just until I can figure out this mutability stuff
}

通过这些更改,我已经能够将其提高到 运行,但这对我来说似乎是错误的。

loop {
    if !has_location(&mut locations, &next_checkpoint_x, &next_checkpoint_y) {
        // stuff
    }
}
fn has_location(location_list: &LinkedList<Location>, target_x: &i32, target_y: &i32) -> bool {
    true
}

我不希望 has_location 能够改变链表,我只希望它能够借用它,以便它可以查看它的内部。我不想考虑影响它检查的链表的 has_location (或类似函数)。这就是我创建 location_set 的原因。我想要一些东西以只读方式引用位置,并将其传递给 has_location 函数,并且要在调用 has_location 函数后不销毁所引用的内容(位置)。我在函数调用的参数传递中包含 & ,因为我不想传递的参数被销毁 - 所以我想借用它们?

我想要的东西是否有意义 - 如果我最初将位置声明为可变链表,我可以将它的不可变版本传递给函数进行评估吗?

如果您有可变借用,您可以随时重新借用它作为不可变借用。这甚至会隐含地发生(&mut T 将强制转换为 &T)。因此,您可以直接将 location_set 传递给 has_location。或者如果你想明确表示函数不会改变它的参数,你可以写 &*location_set 而不是 location_set (尽管我觉得这是不必要的)。

另请注意,当存在不可变借用时,您不能使用可变借用;不可变借用在范围内冻结数据结构。同样,当变量在范围内有可变借用时,您不能使用该变量。在您的第一个代码示例中,当 location_set 在范围内时,您不能引用 locations,因为 location_setlocations 上进行可变借用,但您可以只使用 location_set,因为 push_back 只需要一个可变借用(它不需要 LinkedList 按值)。

仅检查数据结构的函数通常会收到对数据结构的不可变借用。如果数据结构改为按值传递,函数将取得它的所有权并因此在返回之前销毁它(除非它被移动到别处)。因此,是的,您希望 has_location 接受对 LinkedList 的不可变借用。通过接受不可变借用(与可变借用相对),编译器将阻止您修改 LinkedList(除非您使用不安全代码)。

综合起来:

use std::collections::LinkedList;

struct Location {
    x: i32,
    y: i32,
}

fn main() {
    let mut locations = LinkedList::new();
    let next_checkpoint_x = 0;
    let next_checkpoint_y = 0;

    loop {
        let location_set = &mut locations;
        // Scanner stuff happens.
        if !has_location(location_set, &next_checkpoint_x, &next_checkpoint_y) {
            let point = Location { x: next_checkpoint_x, y: next_checkpoint_y };
            location_set.push_back(point);
        }
    }
}

fn has_location(location_list: &LinkedList<Location>, target_x: &i32, target_y: &i32) -> bool {
    true
}

Something I don't understand though is in your example, location_set is passed to has_location directly (so the function owns it, right?). This in my mind means that at the end of has_location's scope, location_set should be destroyed, no? How does location_set continue to exist to be used in the if block?

不,has_location 不拥有 location_set。如果这是未实现 Copy 的任何其他类型(例如 String),那么您是对的,但引用具有特殊规则以使其更易于使用。

当您传递对函数的引用时,编译器会自动重新借用该引用以生成新的引用,通常生命周期较短。在这里,编译器正在重新借用可变引用并生成不可变引用;在不可变引用超出范围之前,不能使用可变引用(这里不可变引用未绑定到变量,因此您不会真正注意到这一点)。从概念上讲,就好像您将不可变引用传递给可变引用(在 Rust 中,& &mut T 不允许您改变 T,因为该外部引用可能有多个副本),只是两个引用是"flattened".

Also if location_set is immutable, how is push_back able to add to the end of the list, is it because the function coerces the mutable borrow into an immutable borrow?

location_set 仍然是可变引用(因为我们使用 &mut 运算符创建它)。 has_location 在不可变引用上运行的事实并没有改变 location_set 是可变引用的事实。一旦评估了对 has_location 的调用,location_set 就可以作为可变引用重新使用,因此允许 push_back 等变异操作。

记住可变性是 Rust 纯粹的编译时概念; mut 或缺少 mut 只是让编译器验证您的代码没有进行非法操作,但是一旦您的代码被编译,这些标记就无处可见。