Rust defines reference variables in four ways:
1. a: &T
2. a:&mut T
3. mut a:&T
4. mut a:&mut T
The following describes these four writing methods respectively
1,a: &T
This kind of reference variable means that the reference is immutable and the content pointed to by the reference cannot be modified.
for instance:
#[derive(Debug)] struct Account{ count:u32 } fn main() { let a:&Account = &Account{count:11}; println!("{r:p},{r:?}",r=a); }
Final print:
0x5626a883f000,Account { count: 11 }
In this example, a is a reference to a structural Account, and the content referenced by a cannot be modified. What happens if it is modified? Take the following example:
#[derive(Debug)] struct Account{ count:u32 } fn main() { let a:&Account = &Account{count:11}; a.count = 12; println!("{r:p},{r:?}",r=a); }
At this time, the compiler will report an error:
Compiling playground v0.0.1 (/playground) error[E0594]: cannot assign to `a.count`, which is behind a `&` reference --> src/main.rs:10:3 | 9 | let a:&Account = &Account{count:11}; | ------------------ help: consider changing this to be a mutable reference: `&mut Account{count:11}` 10 | a.count = 12; | ^^^^^^^^^^^^ `a` is a `&` reference, so the data it refers to cannot be written
As can be seen from the error message (` a ` is a ` & ` reference, so the data it references to cannot be written) the data referenced by a cannot be modified.
What if I regenerate an Account and let a reference it? Take the following example:
#[derive(Debug)] struct Account{ count:u32 } fn main() { let a:&Account = &Account{count:11}; let b:&Account = &Account{count:12}; a = b; println!("{r:p},{r:?}",r=a); }
At this time, the compiler will report an error:
error[E0384]: cannot assign twice to immutable variable `a` --> src/main.rs:11:3 | 9 | let a:&Account = &Account{count:11}; | - | | | first assignment to `a` | help: consider making this binding mutable: `mut a` 10 | let b:&Account = &Account{count:12}; 11 | a = b; | ^^^^^ cannot assign twice to immutable variable
It can be seen from the error message that a is an immutable variable and cannot be re assigned.
Summary: A: & T means that the reference is immutable and the content pointed to by the reference cannot be modified.
2,a:&mut T
This kind of reference variable means that the reference is immutable and the content pointed to by the reference can be modified.
for instance:
#[derive(Debug)] struct Account{ count:u32 } fn main() { let a:&mut Account = &mut Account{count:11}; a.count = 12; println!("{r:p},{r:?}",r=a); }
Print now:
0x7ffe8075ba54,Account { count: 12 }
If you want to change the direction of a:
#[derive(Debug)] struct Account{ count:u32 } fn main() { let a:&mut Account = &mut Account{count:11}; let b:&mut Account = &mut Account{count:12}; a = b; println!("{r:p},{r:?}",r=a); }
The same error will be reported:
error[E0384]: cannot assign twice to immutable variable `a` --> src/main.rs:11:3 | 9 | let a:&mut Account = &mut Account{count:11}; | - | | | first assignment to `a` | help: consider making this binding mutable: `mut a` 10 | let b:&mut Account = &mut Account{count:12}; 11 | a = b; | ^^^^^ cannot assign twice to immutable variable
Description a cannot be reset.
Summary: A: & mut t indicates that the reference is immutable and the content pointed to by the reference can be modified.
3,mut a:&T
This kind of reference variable means that the reference can be re assigned, but the content pointed to by the reference cannot be modified.
Here is a normal example:
#[derive(Debug)] struct Account{ count:u32 } fn main() { let mut a:&Account = &Account{count:11}; let b:&Account = &Account{count:12}; a = b; println!("{r:p},{r:?}",r=a); }
The printing results are as follows:
0x5615b6e50004,Account { count: 12 }
If we try to modify the content of the Account:
#[derive(Debug)] struct Account{ count:u32 } fn main() { let mut a:&Account = &Account{count:11}; a.count = 12; println!("{r:p},{r:?}",r=a); }
You will get the following error:
error[E0594]: cannot assign to `a.count`, which is behind a `&` reference --> src/main.rs:10:3 | 9 | let mut a:&Account = &Account{count:11}; | ------------------ help: consider changing this to be a mutable reference: `&mut Account{count:11}` 10 | a.count = 12; | ^^^^^^^^^^^^ `a` is a `&` reference, so the data it refers to cannot be written
Note the content pointed to by a cannot be modified.
Summary: mut A: & T , indicates that the reference is variable, but the content pointed to by the reference cannot be modified.
4,mut a:&mut T
After the discussion of the above three situations, we can know that this writing actually means that the reference is variable and the content pointed to by the reference can be modified.
As for the example, leave it to the reader to try~