gpt4 book ai didi

rust - 不能借用为不可变的,因为它在函数参数中也被借用为可变的

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

这里发生了什么 ( playground )?

struct Number {
num: i32
}

impl Number {
fn set(&mut self, new_num: i32) {
self.num = new_num;
}
fn get(&self) -> i32 {
self.num
}
}

fn main() {
let mut n = Number{ num: 0 };
n.set(n.get() + 1);
}

给出这个错误:

error[E0502]: cannot borrow `n` as immutable because it is also borrowed as mutable
--> <anon>:17:11
|
17 | n.set(n.get() + 1);
| - ^ - mutable borrow ends here
| | |
| | immutable borrow occurs here
| mutable borrow occurs here

但是,如果您只是将代码更改为这样,它就可以工作:

fn main() {
let mut n = Number{ num: 0 };
let tmp = n.get() + 1;
n.set(tmp);
}

对我来说,它们看起来完全相同 - 我的意思是,我希望前者在编译期间转换为后者。 Rust 不会在评估下一级函数调用之前评估所有函数参数吗?

最佳答案

这一行:

n.set(n.get() + 1);

脱糖成

Number::set(&mut n, n.get() + 1);

错误信息现在可能更清楚了:

error[E0502]: cannot borrow `n` as immutable because it is also borrowed as mutable
--> <anon>:18:25
|
18 | Number::set(&mut n, n.get() + 1);
| - ^ - mutable borrow ends here
| | |
| | immutable borrow occurs here
| mutable borrow occurs here

当 Rust 从左到右评估参数时,该代码等同于:

let arg1 = &mut n;
let arg2 = n.get() + 1;
Number::set(arg1, arg2);

Editor's note: This code example gives an intuitive sense of the underlying problem, but isn't completely accurate. The expanded code still fails even with non-lexical lifetimes, but the original code compiles. For the full description of the problem, review the comments in the original implementation of the borrow checker

现在应该很明显出了什么问题。交换前两行可以解决这个问题,但 Rust 不会进行那种控制流分析。

这最初创建为 bug #6268 , 现在集成到RFC 2094 , non-lexical-lifetimes .如果您使用 Rust 1.36 或更新版本,NLL 会自动启用并且 your code will now compile without an error .

另见:

关于rust - 不能借用为不可变的,因为它在函数参数中也被借用为可变的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50621299/

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