This actually already works with functional record update syntax:
use std::default::Default;
#[deriving(Default)]
struct Struct;
fn main() {
let my_struct = Struct { .. Default::default() };
}
So it’d basically be sugar for that pattern. A function default() in the prelude that called Default::default() would be super useful, as it’d save an import and some noise. Optimally, the sugar should work with structs that don’t implement Default, and instead should look at the omitted fields for implementation, throwing a compiler error if there’s a field that doesn’t have it.
On a related note, it would be awesome if unit structs automatically implemented Default since there’s no other state they could possibly be initialized with. Then with this syntax, it would be a lot cleaner:
use std::kinds::marker::NoCopy, NoSend, NoSync;
struct Struct {
nocopy: NoCopy,
nosend: NoSend,
nosync: NoSync,
}
let mystruct = Struct {
nocopy: NoCopy, // Is this really necessary?
nosend: NoSend,
nosync: NoSync,
};
// versus
let mystruct = Struct { .. };
// or
let mystruct = Struct { .. default() };