# Pre-RFC: syntactic sugar for initializing fields with the same expression

**URL:** <https://internals.rust-lang.org/t/pre-rfc-syntactic-sugar-for-initializing-fields-with-the-same-expression/21102>\
**Category:** language design\
**Created:** [June 30, 2024, 4:22pm UTC](https://internals.rust-lang.org/t/pre-rfc-syntactic-sugar-for-initializing-fields-with-the-same-expression/21102 "2024-06-30T16:22:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![binarycat](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/binarycat/32/12210_2.png) [@binarycat](https://internals.rust-lang.org/u/binarycat)\
**Post date:** [June 30, 2024, 4:22pm UTC](https://internals.rust-lang.org/t/pre-rfc-syntactic-sugar-for-initializing-fields-with-the-same-expression/21102/1 "2024-06-30T16:22:59Z")

</div>

i've see a few proposals for making it easier to work with optional fields and partial initialization, so here's my idea.

it's mainly inspired by the shorthand for initializing arrays, and the existing `..` struct syntax.

here's a example of what this might look like:

```rust
struct User {
  first_name: Option<String>,
  last_name: Option<String>,
  post_count: u64,
  id: Uuid,
}

impl User {
  fn new() -> Self {
    User { id: Uuid::new_random(), ..: Default::default() }
  }
}

```

the `..:` syntax\[1\] would initialize all fields not previously named with the given value, in this case `Default::default`. if the expression is not valid for all fields, it will give a typecheck error.

this would be useful in a lot of cases, not just for when you want a partial default, but also if you want to expose the all-default case in some sort of `empty()` method, but provide a more useful value for the actual `Default` trait impl. you could even use it for easily initializing a pure diagonal vector, handy for things like scaling a 3d model evenly across all axes.

like `..`, this syntax would not be useable on structs with fields that are not in scope\[2\]

* * *

1. this might be too similar to the `..` syntax but i can't think of anything better 

2. as well as non\_exhaustive structs that are defined in other crates

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [June 30, 2024, 6:00pm UTC](https://internals.rust-lang.org/t/pre-rfc-syntactic-sugar-for-initializing-fields-with-the-same-expression/21102/2 "2024-06-30T18:00:01Z")

</div>

Without a potential syntax which is more distinct than `..:`, it's a very hard sell. Before any work into something like this, I'd much prefer following through the proposal to have per-field defaults available, which addresses much the same use cases.

That, plus there's some discussion on whether FRU should be changed from moving _from_ the `rest` place to instead moving _into_ the `rest` place.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [September 28, 2024, 6:00pm UTC](https://internals.rust-lang.org/t/pre-rfc-syntactic-sugar-for-initializing-fields-with-the-same-expression/21102/3 "2024-09-28T18:00:43Z")

</div>

Closed on September 28, 2024, 6:00pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
