this is error:
struct A(i32);
fn main(){
let a = A(1);
let ra = &a;
let x = *ra;
}
but this is ok(only change &
to Box::new
):
struct A(i32);
fn main(){
let a = A(1);
let ra = Box::new(a);
let x = *ra;
// (1)
}
what is happenning here?
when I add this at (1):
let ra2 = ra;
it say "use of moved value: ra
". But I just move the *ra
.
if in seconed code it moves ra, then in first code why it doesn't move ra?
I write my box(via The Rust Programming Language 15.2), but it also cannot moves ra, and report an error:
use std::ops::Deref;
struct A(i32);
struct MyBox<T>(T);
impl<T> MyBox<T> {
fn new(x: T) -> MyBox<T> {
MyBox(x)
}
}
impl<T> Deref for MyBox<T> {
type Target = T;
fn deref(&self) -> &T {
&self.0
}
}
fn main(){
let a = A(1);
let ra = MyBox::new(a);
let x = *ra;
}