【发布时间】:2020-10-19 01:01:59
【问题描述】:
具有借用可变引用的函数如何调用具有相同借用可变引用的第二个函数?
fn main() {
let mut a = vec![0, 1, 2, 3, 4];
first_function(&mut a);
println!("{:?}", a);
}
fn first_function(a: &mut Vec<i32>) {
println!("...first function");
a[0] = 5;
second_function(&mut a);
}
fn second_function(a: &mut Vec<i32>) {
println!("...second function");
a[2] = 6;
}
编译器错误通常很有帮助,但我不明白这个;
error[E0596]: cannot borrow `a` as mutable, as it is not declared as mutable
--> src/main.rs:12:21
|
9 | fn first_function(a: &mut Vec<i32>) {
| - help: consider changing this to be mutable: `mut a`
...
12 | second_function(&mut a);
| ^^^^^^ cannot borrow as mutable
...这是Rust Playground中代码的链接
【问题讨论】:
标签: rust