# Shorthand for \`Default::default()\`

**URL:** <https://internals.rust-lang.org/t/shorthand-for-default-default/17916>\
**Category:** language design\
**Created:** [December 6, 2022, 12:55am UTC](https://internals.rust-lang.org/t/shorthand-for-default-default/17916 "2022-12-06T00:55:31Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![earthengine](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/earthengine/32/2577_2.png) [@earthengine](https://internals.rust-lang.org/u/earthengine)\
**Post date:** [December 6, 2022, 12:55am UTC](https://internals.rust-lang.org/t/shorthand-for-default-default/17916/1 "2022-12-06T00:55:31Z")

</div>

Is there anyone proposed something similar to this?

```rust
foo(v, ~, ~, 10);

```

desugar to

```rust
foo(v, <_ as Default>::default(), <_ as Default>::default(),10);

```

Today with macros the best we can do is

```rust
foo(v, d!(), d!(), 10);

```

where `d!()` is defined as the above but since it is short it is likely to collide with definitions.

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)\
**Post date:** [December 6, 2022, 1:04am UTC](https://internals.rust-lang.org/t/shorthand-for-default-default/17916/2 "2022-12-06T01:04:36Z")

</div>

I doubt this would get anything as short as just `~`.

Maybe this could be in the prelude

```rust
use std::default::Default::default;

```

(once that's possible).

Then it'd be

```rust
foo(v, default(), default(), 10);

```

which seems tolerable.

Of course, what I'd really want there is

> [@Pre-pre-RFC: syntactic sugar for \`Default::default()\`](https://internals.rust-lang.org/t/pre-pre-rfc-syntactic-sugar-for-default-default/13234/75):
>
> Thanks @spacekookie! I know is a long thread so it is a good time to summarize what the RFC I'll be writing will propose, particularly because it is removed from what I originally wrote: Language support for default values on structs through const expressions: struct Car { wheel\_count: usize = 4; } Language support for partially defaulted structs: struct Person { name: String, age: i128 = 0; } Teach #[derive(Default)] to use the default values #[derive(Default)] struct P…

because once you have two "I don't care just do whatever" parameters you'll probably have more soon, so the best thing is to make it `foo(v, .{ width: 10, .. })` instead of spamming in lots of `~, ~, ~, ~`.

---

<div class="post-metadata">

**Author:** ![earthengine](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/earthengine/32/2577_2.png) [@earthengine](https://internals.rust-lang.org/u/earthengine)\
**Post date:** [December 6, 2022, 1:27am UTC](https://internals.rust-lang.org/t/shorthand-for-default-default/17916/3 "2022-12-06T01:27:55Z")

</div>

> [@scottmcm](#):
>
> because once you have two "I don't care just do whatever" parameters you'll probably have more soon, so the best thing is to make it `foo(v, .{ width: 10, .. })` instead of spamming in lots of `~, ~, ~, ~`.

The `..` grammar is fantastic for structs since the fields are generally named. However for function arguments or tuple structs, the `~` grammar can precisely specify which location to be defaulted - and functions/tuples should not have too much arguments (like \> 10), so there wouldn't be too much `~` to type.

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [December 6, 2022, 2:20am UTC](https://internals.rust-lang.org/t/shorthand-for-default-default/17916/4 "2022-12-06T02:20:38Z")

</div>

> [@earthengine](#):
>
> Today with macros the best we can do is
> 
> ```rust
> foo(v, d!(), d!(), 10);
> 
> ```

Why macros? Functions can be generic, you know?

```rust
#![feature(default_free_fn)]
use std::default::default as d;

```

(or [define](https://doc.rust-lang.org/1.65.0/src/core/default.rs.html#177) it yourself when using stable)

---

<div class="post-metadata">

**Author:** ![earthengine](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/earthengine/32/2577_2.png) [@earthengine](https://internals.rust-lang.org/u/earthengine)\
**Post date:** [December 7, 2022, 2:40am UTC](https://internals.rust-lang.org/t/shorthand-for-default-default/17916/5 "2022-12-07T02:40:44Z")

</div>

> [@steffahn](#):
>
> Why macros? Functions can be generic, you know?

Thanks for the advise. My consideration is that functions are far more common in Rust and a person to write the code will have to ensure there is no variables `d` in scope, especially when it is a closure.

This is what I said "name collision" (sorry for the misspell) in the top post. Since macros are less used and has its own grammar noise `!`, it seems to be better.

Another option would be, regardless it is a macro or not, use a Unicode symbol to be the identifier. Would look like

```rust
#![allow(uncommon_codepoints)]

fn ࠚ‎<T>() -> T
where
    T: Default,
{
    <T as Default>::default()
}
foo(v, ࠚ‎(), ࠚ‎(), 10);

```

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [December 7, 2022, 8:57am UTC](https://internals.rust-lang.org/t/shorthand-for-default-default/17916/6 "2022-12-07T08:57:47Z")

</div>

> [@earthengine](#):
>
> My consideration is that functions are far more common in Rust and a person to write the code will have to ensure there is no variables `d` in scope, especially when it is a closure.

Oh, that makes sense. I hadn't thought of that; yes, macros are way less likely to get into a naming conflict. Although naturally, that would still seem like a stylistically odd/questionable choice for why to use a macro.

---

<div class="post-metadata">

**Author:** ![simonbuchan](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/simonbuchan/32/9390_2.png) [@simonbuchan](https://internals.rust-lang.org/u/simonbuchan)\
**Post date:** [December 7, 2022, 8:59am UTC](https://internals.rust-lang.org/t/shorthand-for-default-default/17916/7 "2022-12-07T08:59:10Z")

</div>

There's [default\_free\_fn - The Rust Unstable Book](https://doc.rust-lang.org/beta/unstable-book/library-features/default-free-fn.html)

---

<div class="post-metadata">

**Author:** ![earthengine](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/earthengine/32/2577_2.png) [@earthengine](https://internals.rust-lang.org/u/earthengine)\
**Post date:** [December 7, 2022, 10:13am UTC](https://internals.rust-lang.org/t/shorthand-for-default-default/17916/8 "2022-12-07T10:13:20Z")

</div>

> [@scottmcm](#):
>
> Of course, what I'd really want there is
> 
> Thanks @spacekookie! I know is a long thread so it is a good time to summarize what the RFC I'll be writing will propose, particularly because it is removed from what I originally wrote: Language support for default values on structs through const expressions: struct Car { wheel\_count: usize = 4; } Language support for partially defaulted structs: struct Person { name: String, age: i128 = 0; } Teach #[derive(Default)] to use the default values #[derive(Default)] struct P…

I read through the post and just figured out a bigger problem.

Today,

```rust
let foo = Foo { field1: bar(), ..my_default‎() };

```

hypothetically expends to (assuming `my_default` is generic)

```rust
let mut foo: Foo = my_default<Foo>();
foo.field1 = bar();
let foo = foo; // Remove the `mut`

```

This could be problematic since we might not want to write such a thing (and if `my_default` is `Default::default()` within the `<Foo as Default>::default()` implementation, it is infinitely recursive). We would like it to be expanded to

```rust
let foo = Foo {
    field1: bar(),
    field2: my_default<_>(),
    field3: my_default<_>(),
    ...
};

```

So I think we might need another form of the defaulting sugar

```rust
let foo = Foo { field1: bar(), ...my_default() };

```

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [December 7, 2022, 4:40pm UTC](https://internals.rust-lang.org/t/shorthand-for-default-default/17916/9 "2022-12-07T16:40:24Z")

</div>

> [@earthengine](#):
>
> Today,
> 
> ```rust
> let foo = Foo { field1: bar(), ..my_default‎() };
> 
> ```
> 
> hypothetically expends to (assuming `my_default` is generic)
> 
> ```rust
> let mut foo: Foo = my_default<Foo>();
> foo.field1 = bar();
> let foo = foo; // Remove the `mut`
> 
> ```

Small note: that's not exactly the correct desugaring. It doesn't move the whole `my_default()` value and then update `field1`; but it constructs the `my_default()` in a temporary variable and then creates a _new_ instance of the struct using `bar()` for `field1` and all the other fields are initialized by copying/moving them from the temporary.

This difference is admittedly more relevant in cases where the `..expr` refers to an existing place in memory and not a newly constructed value, but even here, it better explains certain limitations such as that all fields need to be public.

---

<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:** [March 7, 2023, 4:40pm UTC](https://internals.rust-lang.org/t/shorthand-for-default-default/17916/10 "2023-03-07T16:40:52Z")

</div>

Closed on March 7, 2023, 4:40pm UTC

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