a是一个Vec<i32>可以在一个表达式中可变且不可变地引用的 a :
fn main() {
let mut a = vec![0, 1];
a[0] += a[1]; // OK
}
Run Code Online (Sandbox Code Playgroud)
我认为这个编译是因为i32implements Copy,所以我创建了另一种类型,Copy它像第一个例子一样实现和编译它,但它失败了:
use std::ops::AddAssign;
#[derive(Clone, Copy, PartialEq, Debug, Default)]
struct MyNum(i32);
impl AddAssign for MyNum {
fn add_assign(&mut self, rhs: MyNum) {
*self = MyNum(self.0 + rhs.0)
}
}
fn main() {
let mut b = vec![MyNum(0), MyNum(1)];
b[0] += b[1];
}
Run Code Online (Sandbox Code Playgroud)
error[E0502]: cannot borrow `b` as immutable because it is also borrowed …Run Code Online (Sandbox Code Playgroud) 为什么下面的代码不能编译(playground):
use std::collections::HashMap;
fn main() {
let mut h: HashMap<u32, u32> = HashMap::new();
h.insert(0, 0);
h.insert(1, h.remove(&0).unwrap());
}
Run Code Online (Sandbox Code Playgroud)
借阅检查员抱怨说:
error[E0499]: cannot borrow `h` as mutable more than once at a time
--> src/main.rs:6:17
|
6 | h.insert(1, h.remove(&0).unwrap());
| - ------ ^ second mutable borrow occurs here
| | |
| | first borrow later used by call
| first mutable borrow occurs here
Run Code Online (Sandbox Code Playgroud)
然而,代码是安全的,最后一行的几乎机械转换使它编译(playground):
//h.insert(1, h.remove(&0).unwrap());
let x = h.remove(&0).unwrap();
h.insert(1, x);
Run Code Online (Sandbox Code Playgroud)
我的理解是,这种问题可以通过非词法生命周期解决。这个问题是一个例子,还有很多其他问题。 …
我知道一般的答案-您只能一次或多次多次可变借贷,但不能两次都借。我想知道为什么将这种特定情况视为同时借款。
我有以下代码:
fn main() {
let mut v = vec![1, 2, 3, 4, 5];
let n = 3;
// checks on n and v.len() and whatever else...
let mut s = v[..n].to_vec();
for i in 0..n {
v[i + v.len() - n] = s[1];
}
}
Run Code Online (Sandbox Code Playgroud)
在1.36.0下会产生以下错误:
fn main() {
let mut v = vec![1, 2, 3, 4, 5];
let n = 3;
// checks on n and v.len() and whatever else...
let mut s = v[..n].to_vec();
for i in …Run Code Online (Sandbox Code Playgroud) 当可变参数作为函数参数传递时,借用检查器不允许将其用于构造其他参数,即使这些参数克隆值而不持有引用也是如此。
虽然在函数外部分配变量始终是一个选项1,但从逻辑上讲,这似乎过于热心,借用检查器可以考虑这一点。
这是按预期工作还是应该解决?
#[derive(Debug)]
struct SomeTest {
pub some_value: f64,
pub some_other: i64,
}
fn some_fn(var: &mut SomeTest, other: i64) {
println!("{:?}, {}", var, other);
}
fn main() {
let mut v = SomeTest { some_value: 1.0, some_other: 2 };
some_fn(&mut v, v.some_other + 1);
// However this works!
/*
{
let x = v.some_other + 1;
some_fn(&mut v, x);
}
*/
}
Run Code Online (Sandbox Code Playgroud)
给出这个错误:
#[derive(Debug)]
struct SomeTest {
pub some_value: f64,
pub some_other: i64,
}
fn some_fn(var: &mut …Run Code Online (Sandbox Code Playgroud) 我有以下无法编译的代码:
struct A {
x: i32,
}
impl A {
fn add_assign(&mut self, other: &Self) {
self.x += other.x;
}
fn double(&mut self) {
self.add_assign(self);
}
}
Run Code Online (Sandbox Code Playgroud)
错误是:
error[E0502]: cannot borrow `*self` as mutable because it is also borrowed as immutable
--> src/lib.rs:11:9
|
11 | self.add_assign(self);
| ^^^^^----------^----^
| | | |
| | | immutable borrow occurs here
| | immutable borrow later used by call
| mutable borrow occurs here
Run Code Online (Sandbox Code Playgroud)
如何传递self作为参数add_assign?我已经试过&self, …