Hello,
The problem :
It seems that it is possible to display the value of a mutable variable having a mutable reference.
I say this because according to a certain video, using println! to display a variable, will create an immuatable reference to that same variable. However, we know that we can't have a mutable and an immuatable reference at the same time.
The video is this one (at 3min 18) : https://youtu.be/u4KyvRGKpuI?t=198
and in the video, the person gets an error.
The question :
I tried to display a mutable variable that had a mutable reference. I used println! and it worked, the mutable variable named country
has been successfuly displayed, even as it was mutably referenced. So how would it be possible for pintln! to assign an immutable reference to an already mutably referenced variable without this generating an error during error control ?
for example :
fn main()->() {
let empty = ""; //don't account for this variable as it is only there as a mean of better displayment when using println!
let mut country = "Arabia";
println!("{0: <10}{1:><30} original",country,empty);
country = "Japan";
println!("{0: <10}{1:><30} mod1",country,empty);
println!("|");
println!("|");
println!("|");
println!("|");
let reference = &mut country;
println!("{reference: <10}{empty:><30} reference to mod1");
*reference = "Germany";
println!("{country: <10}{empty:><30} reference to mod2");
}
If you make this program run, it will in effect display the variable country
without errors.
This is strange because right before, on the line let reference = &mut country;
we clearly see that the new variable reference
is a mutable reference for the variable country
.
Despite all this, the line println!("{country: <10}{empty:><30} reference to mod2");
sill works and displays the value Germany
Thanks and have a good day