【发布时间】:2021-05-09 19:22:47
【问题描述】:
编译这段代码:
fn foo<'a>(f: fn(&'a mut i32), x: &'a mut i32) {
f(x);
f(x);
}
我收到以下错误:
error[E0499]: cannot borrow `*x` as mutable more than once at a time
--> src\main.rs:3:7
|
1 | fn foo<'a>(f: fn(&'a mut i32), x: &'a mut i32) {
| -- lifetime `'a` defined here
2 | f(x);
| ----
| | |
| | first mutable borrow occurs here
| argument requires that `*x` is borrowed for `'a`
3 | f(x);
| ^ second mutable borrow occurs here
error: aborting due to previous error
For more information about this error, try `rustc --explain E0499`.
error: could not compile `scratch`
To learn more, run the command again with --verbose.
但是如果我把f的类型从fn(&'a mut i32)改成fn(&mut i32),就编译成功了,这是为什么呢?
【问题讨论】: