Permissions-Array
mutable reference effects on permissions
cases | Comment |
---|---|
b = &a; | |
b = &mut a; | |
mut b = &a; | |
mut b = &mut a; |
b = &a;
- a is declared.
- b takes ownership of a but only with R permission.
- both a and b are accessible in R only.
- once b is last used in the current scope, a will recover its W permission.
fn main() { let mut a: [u8; 3] = [2; 3]; let b = &a; // b[0] += 1; println!("{:?}", b); println!("{:?}", a); }
b = &mut a;
- a is declared.
- b takes ownership of a but only with R permission.
- b are accessible inre R only but a lose the R permission.
- once b is last used in the current scope, a will recover its W permission.
fn main() { let mut a: [u8; 3] = [2; 3]; let b = &mut a; b[0] += 1; println!("{:?}", b); println!("{:?}", a); }
mut b = &a;
- a is declared.
- b takes ownership of a but only with R permission.
- b is reassigned to mut reference to c.
- b’s permission still is defined from the first assignment
Here a weird behaviour, since b is mutable, it should be possible to reassign it to a reference of mutable c. but
the borrow checker do not agree with it. Is a feature or a bug?
fn main() { let mut a: [u8; 3] = [2; 3]; let mut b = &a; let mut c: [u8; 3] = [1; 3]; //b[0] += 1; b = &mut c; //b[0] += 1; println!("{:?}", b); println!("{:?}", a); }
When reassiging the reference it must have the same size of the first assignment.
fn main() { let mut a: [u8; 3] = [2; 3]; let mut b = &a; let mut c: [u8; 3] = [1; 3]; b = &mut c; }
mut b = &mut a;
- a is declared.
- b takes ownership of a but with W permission.
- b is reassigned to mut reference to c
- b type is decided from the first assignment
Here a weird behaviour, since b is mutable, it should be possible to reassign it to a reference of mutable c. but
the borrow checker do not agree with it. Is a feature or a bug?
fn main() { let mut a: [u8; 3] = [2; 3]; let mut b = &mut a; let mut c: [u8; 3] = [1; 3]; b[0] += 1; b = &mut c; b[0] += 1; println!("{:?}", b); println!("{:?}", a); }
variable scope
Since arrays are store in the stack the behaviour is similar to the scalar variables.
fn main() { let mut a: [u8; 3] = [2; 3]; println!("{:?}", a); { let b = &mut a; b[0] += 1; println!("{:?}", b); } // println!("{:?}," b); // b is out of the scope and the memory is freed. println!("{:?}", a); }