3 ms·
I hope that one day the default struct field values RFC will be reconsidered: https://github.com/rust-lang/rfcs/pull/1806 https://github.com/rust-lang/rfcs/pull
by nitsky 5y ago
I hope that one day the default struct field values RFC will be reconsidered: https://github.com/rust-lang/rfcs/pull/1806 https://github.com/rust-lang/rfcs/pull/1806.
This would allow a syntax more familiar to users of JavaScript, where fields with their default value can be omitted:
struct Person {
name: String,
email: Option<String> = None,
}
let person = Person {
name: "John Doe".to_owned(),
..
};
- madiyar 5y agoThis is probably obvious and it isn't 100% alternative to Javascript syntax, but you can leverage Default trait to come a bit closer: #[derive(Default)] struct Person { name: String, email: Option<String>, } fn main() { let person = Person { name: "John Doe".to_owned(), ..Default::default() }; }
- kevincox 5y agoIt would be really nice if .. could be a shortcut for ..Default::default() which would match the syntax of pattern matching.
- kangalioo 5y agoThe deal breaker with `..Default::default()` is that it only works for structs without any required fields. Forgetting a required field just means you get the dummy value from Default, that is, if you even manage to implement Default for the struct at all (the required fields may not have a sensible default value)