fn main() {
let mut y = 3;
let f = |x| {
y = 4;
y + x
};
let x = 3;
let yy = f(x);
println!("y={}, yy={}", y, yy);
}
错误:
error[E0596]: cannot borrow `f` as mutable, as it is not declared as mutable
--> closures/src/main.rs:28:14
|
23 | let f = |x| {
| - help: consider changing this to be mutable: `mut f`
...
28 | let yy = f(x);
| ^ cannot borrow as mutable
不明白最后一个错误提示说明啥
1
共 1 条评论, 1 页
评论区
写评论首先你的闭包函数想要修改外部的变量 因此认为是fnMut 然后编译器是提示你让你在闭包赋值的变量前加mut