gpt4 book ai didi

使用rust 错误 : borrow occurs after drop a mutable borrow

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

<分区>

我的测试代码:

let mut c = 0;
let mut inc = || { c += 1; c };
drop(inc);
println!("{}", c);

rustc 说:

error[E0502]: cannot borrow `c` as immutable because it is also borrowed as mutable
--> .\src\closure.rs:20:24
|
12 | let mut inc = || { c += 1; c };
| -- ----- previous borrow occurs due to use of `c` in closure
| |
| mutable borrow occurs here
...
20 | println!("{}", c);
| ^^^^^ immutable borrow occurs here
21 | }
| - mutable borrow ends here

但是 inc 是在 println! 借用 c 之前手动删除的,不是吗?

那么我的代码有什么问题呢?请帮忙。

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