Add default keyword

In Rust, it is possible to add a default value implementation to a type via the Default trait, and if the default value is bound to a variable, we can do this with the following code:

let value = i32::default();

or Option

let value = Option::<i32>::default();

We can make it more convenient to explicitly declare variables and bind default values by adding a default keyword:

let value:i32 = default;

or Option

let value:Option<i32> = default;

When generic const and const traits are ready in the future, hopefully anyone can define such constant themselves.

const DEFAULT<T: const Default>: T = const { <T as Default>::default() };

let value: i32 = DEFAULT;

This keyword was inspired by C#, see default value expressions

I'm hoping that we can achieve something very similar to this: we can use Default::default; in the prelude, and then write:

let value: SomeType = default();

The value isn't extremely clear in that case, since that isn't much shorter than let value = SomeType::default();, but the value becomes clearer when writing:

some_func(arg, default());

Yes, code can be shortened when in the input and output parameters of a function.

I really want that, but it does sound like a breaking change we need an edition for @josh, do you want to wait till 2027 with that?

There is no breaking change involved, the feature of being able to use associated functions of traits uses syntax that is always a compile error today and adding a new function to the prelude is non-breaking.

(On the other hand making default a keyword is a breaking change and will result in having to use Default::r#default() when calling the method).

Normally for your examples I would write simply

let value = 0;


let value = None;

Frankly I think making default a keyword would be a rather bad idea even if we were starting with a clean sheet, and fiddling with syntax between editions is even worse.

When it is a customized structure with many fields! is not so simple, and the default keyword, can reduce the code a lot.

To the best of my knowledge this wouldn't require an edition. It would require support for use Trait::func;, which is proposed in Add support for `use Trait::func` by obsgolem · Pull Request #3591 · rust-lang/rfcs · GitHub .

1 Like

You can already use ..Default::default() and you can make it even shorter with:

fn default<T: Default>(): T {

as mentioned, an equivalent is somewhat likely to be part of the standard library at some point.

1 Like

Note there already is a default keyword, for (unstable) specialization.

5 posts were merged into an existing topic: Off-topic discussion: Implemention of Default trait / internals vs. user forum

That's a good idea too. :heart:

Given how much use of Default there already is, I can't imagine that wouldn't be a contextual keyword?

It definitely is, or you'd be doing stuff like

//           vv
let s = <_>::r#default();

all the time (which the OP suggestion would also require, modulo some new mechanics).

1 Like