2

Here are two examples of dynamic mutable value.

pub fn main() {
    let mut x = String::from("Hello");
    println!("{}", x);

    x = format!("{x}, world!");
    println!("{}", x);
}

So as you can see, we have a mutable variable x. x gets borrowed by format! & turns to a new value that is then assigned back to x.

pub fn main() {
    let mut x = String::from("Hello, world!");
    println!("{}", x);

    x = String::from("Lorem, ipsum!");
    println!("{}", x);
}

My real problem is this code, where the value assigned to x has no connection with it's initial value. Variables can only take owner-ship over a single value, so how does Rust keep track of "Hello, world!" & "Lorem, ipsum!"? Exactly what happens to mutable values after assignments?

My guess is that it checks if the value getting assigned to depends on the first one, if yes, it won't drop it.

0 Answers0