I'm weak on understanding the definitions of "declaration" and "definition" in relation to Rust. The Rust reference says that struct fields are dropped in declaration order. Does this mean in the order of the struct when it is originally created {?definition/declaration?},
struct Example {
x: i32,
y: i32,
}
or when values are actually added to the struct {?definition/declaration?} (which can be in an order different from the original creation),
fn foo -> Example {
Example {
y: 43,
x: 42,
}
}
I'm unclear on which of these is the declaration and which is the definition.
I tried to print a message when y dropped but my code failed to compile.
I don't think there is a distinction between the two for structs in Rust.
In some languages (like C), there may be a difference between declaring that a type exists ("there is some type named Example") and defining the structure of that type ("the type named Example has fields x and y"). However, there is not syntax in Rust that separates them, they are one in the same. The Rust Reference and Rust Book seem to prefer calling it a "definition", though you are right the documentation on destructors says "fields of a struct are dropped in declaration order".
The "declared order" is what you see in the struct's definition. Thus x will be dropped before y. The latter example is instantiating a struct, and if the documentation meant that instead it would call it the "instantiation order", "initialization order", or something similar.
None of the above is to say that the distinction doesn't exist in Rust, just not in respect to structs:
let a; a = 5 is valid) though usually that is called "initializing" instead of "defining" but you would be understood either way.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With