still new to Rust and I am learning about the mutability rules of the language. I am trying to understand if structures, objects, arrays are deeply immutable.
So I have this struct
struct Foo {
bar: String,
}
And I would like to see what the rules for deep mutability are. When creating the struct, the struct must be defined as mut in order for it to have properties changed on it.
let mut f = Foo{
bar: String::from("hello")
};
f.bar = String::from("foo"); // Must be mut to change a property
println!("{:?}", f.bar);
However when I define an immutable struct and move it into a mutable vector, the struct becomes mutable.
let f = Foo{
bar: String::from("hello")
};
let mut strings: Vec<Foo> = vec![f];
strings[0].bar = String::from("foo");
println!("{:?}", strings[0].bar);
I am guessing that f is being copied into the mutable vector (and dropped from it's scope?) and the copy is being reassigned as mutable but I am not really sure.
Can I get some insight into what is going on?