The output is 2 , is it a breaking of the rule?
fn main () {
struct S {
id : u8,
}
impl std::ops::Drop for S {
fn drop(&mut self) {
println!("{}",self.id);
}
}
S{id:1}.id=2;
}
The output is 2 , is it a breaking of the rule?
fn main () {
struct S {
id : u8,
}
impl std::ops::Drop for S {
fn drop(&mut self) {
println!("{}",self.id);
}
}
S{id:1}.id=2;
}
That makes a temporary, sets a field on it, and then that temporary gets dropped. It's the same as
let mut temp = S{id:1};
temp.id = 2;
drop(temp);
What "rule" do you think this is breaking?
the object is default to immutable , temp object seems an exception ?
In reality that rule is about variable names, not values. The same value can even change between mutability as it is moved between variables.
// I'm immutable
let var1 = "Hello ".to_string();
// Now I'm mutable
let mut var2 = var1;
var2.push_str("world!");
// Now I'm immutable again
let var3 = var2;
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.