Mut and reference

Doosan published on
0 min, 0 words

Categories: Post

let     a:     &T
let mut a:     &T
let     a: &mut T
let mut a: &mut T
let a = "hello".to_string();
a = a.add(" world");// this will throw error, cannot assign twice to immutalbe variable
let b = a;//move
let c = &a;// a is moved above line, cannot be borrowed
let d = &b;//borrow
let mut a = "hello".to_string();
let b = &mut a; //a is borrowed as MUTABLE, not moved
b.push(' ');
b.push_str("world");
let mut a = "hello".to_string();
let b = "world".to_string();
a=b; // because a is mut can be assigned twice, b value is moved to a
let a = &mut "hello".to_string();
let b = "world".to_string();
*a = b;