If two or more struct initializers have side effects, is the order
that they run defined? Testing with the code below and Rust 0.5 seems
to show that the order of evaluation is the order that the
initialization occurs in (eg swapping the order of len and val in
packet_read causes get_u16 to be called first). However I could not
find this behavior mentioned in the manual and before I relied on it I
wanted to make sure those semantics were stable and not just a quirk
of the implementation.
Thanks,
Jack
Example follows
fn get_u8() -> u8 { io::println("get_u8"); 42 }
fn get_u16() -> u16 { io::println("get_u16"); 31337 }
struct Packet { len: u8, val: u16 }
fn packet_read() -> Packet {
Packet { // swap next two lines to swap order of evaluation
len: get_u8(),
val: get_u16(),
}
}
fn main() { let _p = packet_read(); }
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev