xyanping
xyanping

Reputation: 31

Rust type annotation on left side of let pattern matching

The compiler will complain that z is moved and can not be referenced anymore:
Rust playground

let mut x = 100 ;
let z = &mut x ;
let z1 = z ;   
*z1 = 200 ; 
println!("{}", z) ;  // <== compile error

error[E0382]: borrow of moved value: z

While the following code works well:
Rust playground

let mut x = 100 ;
let z = &mut x ;
let z1: &mut i32 = z ;  // type annotation added to left side
*z1 = 200 ;
println!("{}", z) ;  

The only different is that the latter has type annotation added to left side of let clause. I wonder what is the different between them.

Upvotes: 2

Views: 57

Answers (0)

Related Questions