我试图在Rust中做一个加法器,但由于一个可变的借项超过1个,我无法设置下一位数字的cin值
这是我的代码
let mut digits = vec![vec![0; 3]; 5]; // [[a, b, cin], ..., [a, b, cin]]
// ... some code here
let mut ans = String::new();
for (i, j) in digits.iter_mut().enumerate() {
let [sum, cout] = adder(j[0], j[1], j[2]);
if i < 4 {
digits[i+1][2] = cout;
}
ans += &sum.to_string();
}
ans = ans.chars().rev().collect::<String>();
它应该将进位结果放入下一位数字的进位中,但是却出现此错误:
error[E0499]: cannot borrow `digits` as mutable more than once at a time
--> src\main.rs:26:13
|
23 | for (i, j) in digits.iter_mut().enumerate() {
| -----------------------------
| |
| first mutable borrow occurs here
| first borrow used here, in later iteration of loop
...
26 | digits[i+1][2] = cout;
| ^^^^^^ second mutable borrow occurs here
有什么办法解决这个问题/解决这个问题?