Immutable type: Can’t change value Mutable type: Can change value

// Non-ref type to immutable reference
    fun scenario_1(){
        let value_a = 10;
        let imm_ref: &u64 = &value_a;
        print(imm_ref);
    }

In this snippet I’m creating an immutable type reference to value_a and then printing it. The print function only takes in references, but since imm_ref is in itself a reference, I can simply just pass it and it should print with no issue.

// Mutable ref to immutable reference
    fun scenario_2(){
        let value_a = 10;
        let mut_ref: &mut u64 = &mut value_a;
        let imm_ref: &u64 = mut_ref;
        print(imm_ref);
    }

In this code snippet I’m first making a non-ref, then using it to create a mutable reference to it. mut_ref is the mutable reference to value_a. Now, I’m using the mutable reference to create the immutable reference, and then printing it using the print function.

//Immutable reference to mutable reference (dereference first)
fun re_assign(value_a: &mut u64, value_b: &u64){
        *value_a = *value_b;
        print(value_a);
    }
  
    fun scenario_3(){
        let value_a: &mut u64 = &mut 10;
        let value_b: &u64 = &20;
        re_assign(value_a, value_b);  
    }

In this code snippet, I’ve created a function called re_assign whose purpose is to take in a mutable reference and an immutable reference, dereference both of them to obtain the values of both, and then assign the immutable reference value to the mutable variable using the mutable reference. It also then prints the mutable reference.

In the scenario_3 function, all I’m doing is making a mutable reference to value 10 and an immutable reference to value 20. I am then passing both of these into the re_assign function for the mutable reference to go from 10 to 20 as intended.