这是示例:
fn bar<T>(v: &mut Vec<T>) {
bar(v); //reborrows
bar(v); //reborrows
}
编译得很好。但考虑稍微修改的版本:
fn foo<T>(v: &mut Vec<T>) {
let mut f = ||{
foo(v);
};
let mut g = ||{
foo(v);
};
f();
g();
}
我们这里遇到的是编译错误:
error[E0524]: two closures require unique access to `*v` at the same time
--> src/lib.rs:5:17
|
2 | let mut f = ||{
| -- first closure is constructed here
3 | foo(v);
| - first borrow occurs due to use of `*v` in closure
4 | };
5 | let mut g = ||{
| ^^ second closure is constructed here
6 | foo(v);
| - second borrow occurs due to use of `*v` in closure
7 | };
8 | f();
| - first borrow later used here
我猜错误消息与同时拥有多个可变引用有关。但在第一个示例中也同时有多个引用。这很令人困惑。