Playing with mutablity.

This commit is contained in:
WickedJack99
2024-09-08 10:37:49 +02:00
parent 33ea81a759
commit 2272aacff2
3 changed files with 88 additions and 0 deletions

7
p_0001_variables/Cargo.lock generated Normal file
View File

@@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "p_0001_variables"
version = "0.1.0"

View File

@@ -0,0 +1,6 @@
[package]
name = "p_0001_variables"
version = "0.1.0"
edition = "2021"
[dependencies]

View File

@@ -0,0 +1,75 @@
fn main() {
//--------------------------------------------------------------------------------------------------------------------------
// Immutable integer
// Declares an 8 bit int variable named "age" and defines it with value of 42.
// Notice: signed int, meaning valid area is -128 to 127
let immutable_age: i8 = 42;
// Notice: Rust compiler checks if values defined are valid for the dedicated variable type.
println!("immutable_age: {}", immutable_age);
println!("--------------------------------------------------------------------------------------------------------------------------");
//--------------------------------------------------------------------------------------------------------------------------
// Mutable integer
let mut mutable_age: i8 = 42;
println!("mutable_age after first definition: {}", mutable_age);
mutable_age = 7;
println!("mutable_age after secon definition: {}", mutable_age);
println!("--------------------------------------------------------------------------------------------------------------------------");
//--------------------------------------------------------------------------------------------------------------------------
// Playing around with references and mutability
{
// Mutable to immutable (shadowing)
let mutable_age: i8 = mutable_age;
// mutable_age = 4; Error shadowing of variable mutable_age makes it immutable.
println!("mutable_age made immutable: {}", mutable_age);
println!("--------------------------------------------------------------------------------------------------------------------------");
}
{
// With & the address of a variable is accessed instead of the value.
let mutable_age_ref: & i8 = & mutable_age;
// age_ref = 42; Error, not able to change value since & i8 is immutable reference, if mutable reference is wanted, use &mut i8.
println!("mutable_age_ref: {}", mutable_age_ref); // TODO I don't get how these are all the same?
println!("mutable_age_ref: {}", & mutable_age_ref); // TODO I don't get how these are all the same?
println!("mutable_age_ref: {}", * mutable_age_ref); // TODO I don't get how these are all the same?
println!("--------------------------------------------------------------------------------------------------------------------------");
}
{
// Reference has to be mutable as well as the address where it is referencing to, to be able to change value of target address.
let mutable_age_ref: &mut i8 = &mut mutable_age;
// Found out by try, rust also uses * to dereference a reference.
*mutable_age_ref = 49;
println!("Mutable age got changed by reference mutable_age_ref: {}", mutable_age);
println!("--------------------------------------------------------------------------------------------------------------------------");
}
{
// Reference has to be mutable as well as the address where it is referencing to, to be able to change value of target address.
let mutable_age_ref: &mut i8 = &mut mutable_age;
let immutable_ref_mutable_age: & i8 = & mutable_age_ref;
// immutable_ref_mutable_age = &4; Error, reference is immutable
println!("immutable_ref_mutable_age: {}", immutable_ref_mutable_age);
println!("--------------------------------------------------------------------------------------------------------------------------");
}
}