Playing with mutablity.
This commit is contained in:
7
p_0001_variables/Cargo.lock
generated
Normal file
7
p_0001_variables/Cargo.lock
generated
Normal 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"
|
||||
6
p_0001_variables/Cargo.toml
Normal file
6
p_0001_variables/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "p_0001_variables"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
75
p_0001_variables/src/main.rs
Normal file
75
p_0001_variables/src/main.rs
Normal 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!("--------------------------------------------------------------------------------------------------------------------------");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user