Without any unsafe code this is simply: let role = Role { name: "basic", flag: 1, disabled: false, }; The language tries to prevent you from interacting with a `Role` object that's not fully initialized. `mem::zero()` could work, but then you'll have to turn the `&'static str` into an `Option ` or a raw pointer, to indicate that it might be null. You could also add `#[derive(Default)]` to the struct, to automatically…
> The language tries to prevent you from interacting with a `Role` object that's not fully initialized. I remember that after reading the Rust book, one of the first things I tried to do was to load a struct from a file. Like pseudocode: struct MY_STRUCT my_struct; read(file, &my_struct, sizeof(my_struct)); 2 lines of code.... It should be simple, right? RiGhT?! Well, the first stackoverflow answer involved unsafe an…
Well, no. If you want to have memory safe subset, you absolutely cannot initialize structs with random bag of bytes in general case. C let's you cut corners here, but in Rust you need to implement (de)serializing logic (no need for unsafe).
>Then I learned that structs are not laid out as declared (OMG!), etc. >It should be simple! I tried to create a variadic function and you can guess how it went.
This is only surprising if you have this weird assumption that things should work like they do in C + some extra.