Demilade Sonuga's blog

All posts

Interlude A: Mutability and Immutability

2022-11-16

Consider the following C-like pseudocode:

struct SomeData {
    char name[10];
    int age;
};

void func() {
    int i = 0;
    SomeData d;
    while(i < 0) {
        printSomeData(address of d);
        i += 1;
    }
}

void printSomeData(address: dAddr) {
    print(*dAddr);
}

The above code listing is in C (ish). In C, all variables are mutable by default, so this is equivalent to:

struct SomeData {
    name: [char; 10],
    age: i32
}

fn func() {
    let mut i = 0;
    let mut SomeData = SomeData::new() // Assuming the `new` function is defined somewhere
    while i < 0 {
        printSomeData()
    }
}