The title seem not very clear about what I met, I wrote an example code:
struct Foo {
i: i32,
}
impl Foo {
fn new() -> Self {
Self {
i: 0
}
}
}
fn main() {
let mut vec = Vec::new();
vec.resize_with(3, || {
Foo::new()
});
let mut vec2 = Vec::new();
for i in 0..3 {
for foo in &mut vec {
foo.i = i;
// let foo2 = unsafe { transmute::<&mut Foo, &Foo>(foo) };
let foo2 = &*foo;
vec2.push(foo2);
}
}
}
As you can see, I want to take a mutable reference of foo
to do some stuff, and save an immutable reference of foo
into another vector.
If I use a normal way to "cast" a mutable reference into immutable, the compiler will complain:
error[E0499]: cannot borrow `vec` as mutable more than once at a time
--> src/main.rs:27:20
|
27 | for foo in &mut vec {
| ^^^^^^^^ `vec` was mutably borrowed here in the previous iteration of the loop
and if I use transmute
to cast it, the compile will pass.
I got a feeling that even if I take a immutable reference foo
by using let foo2 = &*foo;
, it somehow "chains" or "relates" to the mutable foo
, witch relates to the mutable vec
, which invalid the borrow rules.
So how can I do this safely? Or, what could possibly be wrong if I use transmute
?