gpt4 book ai didi

rust - 警告[E0502] : cannot borrow `c` as immutable because it is also borrowed as mutable

转载 作者:行者123 更新时间:2023-11-29 08:31:08 54 4
gpt4 key购买 nike

此代码使用帕斯卡三角形的模数计算组合。

MUsize 是一个在操作中自动求模的结构。

当我运行实现运算符重载的特征存储结构时,出现错误。

当我使用 usize 而不是 MUsize 时,不会出现此警告。

我应该在哪里修改?

我认为一些应该派生到 MUsize 的特征与 usize 相比没有派生。

// pascal's triangle
/*
from AtCoder abc 132
https://www.youtube.com/watch?v=mso8tE1yMl8
*/
/*
1
1 1
1 2 1
1 3 3 1

aCb -> row: a, column: b
*/

static MOD: usize = 1_000_000_000 + 7;
use std::ops::{AddAssign, SubAssign, MulAssign};
use std::ops::{Add, Sub, Mul};
#[derive(Copy, Clone, Debug)]
struct MUsize {x: usize}
impl MUsize {
fn new(x: usize) -> MUsize {
MUsize{x: x%MOD}
}
}
impl AddAssign for MUsize {
fn add_assign(&mut self, other: MUsize) {
let tmp = self.x + other.x;
*self = MUsize {
x: if tmp >= MOD {tmp - MOD} else {tmp}
};
}
}
impl<'a> AddAssign<&'a MUsize> for MUsize {
fn add_assign(&mut self, other: &MUsize) {
let tmp = self.x + other.x;
*self = MUsize {
x: if tmp >= MOD {tmp - MOD} else {tmp}
};
}
}
impl SubAssign for MUsize {
fn sub_assign(&mut self, other: MUsize) {
let tmp = self.x + MOD - other.x;
*self = MUsize {
x: if tmp >= MOD {tmp - MOD} else {tmp}
};
}
}
impl MulAssign for MUsize {
fn mul_assign(&mut self, other: MUsize) {
*self = MUsize {
x: self.x * other.x % MOD
};
}
}
impl Add for MUsize {
type Output = MUsize;
fn add(self, other: MUsize) -> MUsize {
let mut res = MUsize::new(self.x);
res += other.clone();
res
}
}
impl Sub for MUsize {
type Output = MUsize;
fn sub(self, other: MUsize) -> MUsize {
let mut res = MUsize::new(self.x);
res -= other;
res
}
}
impl Mul for MUsize {
type Output = MUsize;
fn mul(self, other: MUsize) -> MUsize {
let mut res = MUsize::new(self.x);
res *= other;
res
}
}

struct C {
c: Vec<Vec<MUsize>>
}
impl C {
fn new(max: usize) -> C {
let mut c = vec![vec![MUsize::new(0); max+2]; max+2];
c[0][0] = MUsize::new(1);
for i in 0..max+1 {
for j in 0..i+1 {
c[i+1][j] += c[i][j];
c[i+1][j+1] += c[i][j];
}
}
C {c}
}
fn c(&self, n: usize, k: usize) -> usize {
self.c[n][k].x
}
}

fn main() {
let c = C::new(40);
println!("{}", c.c(5, 2));
}
   warning[E0502]: cannot borrow `c` as immutable because it is also borrowed as mutable
--> src/main.rs:83:30
|
83 | c[i+1][j] += c[i][j];
| -------------^------
| | |
| | immutable borrow occurs here
| mutable borrow occurs here
| mutable borrow later used here
|
= warning: this error has been downgraded to a warning for backwards compatibility with previous releases
= warning: this represents potential undefined behavior in your code and this warning will become a hard error in the future

warning[E0502]: cannot borrow `c` as immutable because it is also borrowed as mutable
--> src/main.rs:84:32
|
84 | c[i+1][j+1] += c[i][j];
| ---------------^------
| | |
| | immutable borrow occurs here
| mutable borrow occurs here
| mutable borrow later used here
|
= warning: this error has been downgraded to a warning for backwards compatibility with previous releases
= warning: this represents potential undefined behavior in your code and this warning will become a hard error in the future

Finished dev [unoptimized + debuginfo] target(s) in 0.47s

最佳答案

- c[i+1][j] += c[i][j];
- c[i+1][j+1] += c[i][j];

+ let tmp = c[i][j];
+ c[i+1][j] += tmp;
+ c[i+1][j+1] += tmp;

这在没有警告的情况下工作,但这种方式并不酷。

关于rust - 警告[E0502] : cannot borrow `c` as immutable because it is also borrowed as mutable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57582551/

54 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com