Skip to content

Commit 244465d

Browse files
authored
Rollup merge of #73001 - ilya-bobyr:master, r=dtolnay
Free `default()` forwarding to `Default::default()` It feels a bit redundant to have to say `Default::default()` every time I need a new value of a type that has a `Default` instance. Especially so, compared to Haskell, where the same functionality is called `def`. Providing a free `default()` function that forwards to `Default::default()` seems to improve the situation. The trait is still there, so if someone wants to be explicit and to say `Default::default()` - it still works, but if imported as `std::default::default;`, then the free function reduces typing and visual noise.
2 parents 824ea6b + ebb8722 commit 244465d

File tree

3 files changed

+99
-1
lines changed

3 files changed

+99
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# `default_free_fn`
2+
3+
The tracking issue for this feature is: [#73014]
4+
5+
[#73014]: https://github.com/rust-lang/rust/issues/73014
6+
7+
------------------------
8+
9+
Adds a free `default()` function to the `std::default` module. This function
10+
just forwards to [`Default::default()`], but may remove repetition of the word
11+
"default" from the call site.
12+
13+
Here is an example:
14+
15+
```rust
16+
#![feature(default_free_fn)]
17+
use std::default::default;
18+
19+
#[derive(Default)]
20+
struct AppConfig {
21+
foo: FooConfig,
22+
bar: BarConfig,
23+
}
24+
25+
#[derive(Default)]
26+
struct FooConfig {
27+
foo: i32,
28+
}
29+
30+
#[derive(Default)]
31+
struct BarConfig {
32+
bar: f32,
33+
baz: u8,
34+
}
35+
36+
fn main() {
37+
let options = AppConfig {
38+
foo: default(),
39+
bar: BarConfig {
40+
bar: 10.1,
41+
..default()
42+
},
43+
};
44+
}
45+
```

src/libcore/default.rs

+44
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,50 @@ pub trait Default: Sized {
115115
fn default() -> Self;
116116
}
117117

118+
/// Return the default value of a type according to the `Default` trait.
119+
///
120+
/// The type to return is inferred from context; this is equivalent to
121+
/// `Default::default()` but shorter to type.
122+
///
123+
/// For example:
124+
/// ```
125+
/// #![feature(default_free_fn)]
126+
///
127+
/// use std::default::default;
128+
///
129+
/// #[derive(Default)]
130+
/// struct AppConfig {
131+
/// foo: FooConfig,
132+
/// bar: BarConfig,
133+
/// }
134+
///
135+
/// #[derive(Default)]
136+
/// struct FooConfig {
137+
/// foo: i32,
138+
/// }
139+
///
140+
/// #[derive(Default)]
141+
/// struct BarConfig {
142+
/// bar: f32,
143+
/// baz: u8,
144+
/// }
145+
///
146+
/// fn main() {
147+
/// let options = AppConfig {
148+
/// foo: default(),
149+
/// bar: BarConfig {
150+
/// bar: 10.1,
151+
/// ..default()
152+
/// },
153+
/// };
154+
/// }
155+
/// ```
156+
#[unstable(feature = "default_free_fn", issue = "73014")]
157+
#[inline]
158+
pub fn default<T: Default>() -> T {
159+
Default::default()
160+
}
161+
118162
/// Derive macro generating an impl of the trait `Default`.
119163
#[rustc_builtin_macro]
120164
#[stable(feature = "builtin_macro_prelude", since = "1.38.0")]

src/test/ui/resolve/issue-2356.stderr

+10-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,16 @@ error[E0425]: cannot find function `default` in this scope
1414
--> $DIR/issue-2356.rs:31:5
1515
|
1616
LL | default();
17-
| ^^^^^^^ help: try: `Self::default`
17+
| ^^^^^^^
18+
|
19+
help: try
20+
|
21+
LL | Self::default();
22+
| ^^^^^^^^^^^^^
23+
help: consider importing this function
24+
|
25+
LL | use std::default::default;
26+
|
1827

1928
error[E0425]: cannot find value `whiskers` in this scope
2029
--> $DIR/issue-2356.rs:39:5

0 commit comments

Comments
 (0)