gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 08:27:22 26 4
gpt4 key购买 nike

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

我正在处理点,因此如果 (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_sethas_location直接地。或者如果你想明确表示函数不会改变它的参数,你可以写成 &*location_set而不是 location_set (虽然我觉得这是不必要的)。

另外请注意,当不可变借用存在时,您不能使用可变借用;不可变借用在范围内卡住数据结构。同样,当变量在范围内有可变借用时,您不能使用该变量。在您的第一个代码示例中,您不能引用 locations同时location_set在范围内,因为 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 ),那么你是对的,但引用有特殊规则,使它们更方便使用。

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

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只是让编译器验证您的代码没有执行非法操作,但是一旦您的代码被编译,这些标记就无处可见。

关于rust - 借用检查器检查可变引用的最佳实践是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42605042/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com