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.
This commit is contained in:
commit
244465dbb8
@ -0,0 +1,45 @@
|
||||
# `default_free_fn`
|
||||
|
||||
The tracking issue for this feature is: [#73014]
|
||||
|
||||
[#73014]: https://github.com/rust-lang/rust/issues/73014
|
||||
|
||||
------------------------
|
||||
|
||||
Adds a free `default()` function to the `std::default` module. This function
|
||||
just forwards to [`Default::default()`], but may remove repetition of the word
|
||||
"default" from the call site.
|
||||
|
||||
Here is an example:
|
||||
|
||||
```rust
|
||||
#![feature(default_free_fn)]
|
||||
use std::default::default;
|
||||
|
||||
#[derive(Default)]
|
||||
struct AppConfig {
|
||||
foo: FooConfig,
|
||||
bar: BarConfig,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct FooConfig {
|
||||
foo: i32,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct BarConfig {
|
||||
bar: f32,
|
||||
baz: u8,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let options = AppConfig {
|
||||
foo: default(),
|
||||
bar: BarConfig {
|
||||
bar: 10.1,
|
||||
..default()
|
||||
},
|
||||
};
|
||||
}
|
||||
```
|
@ -115,6 +115,50 @@ pub trait Default: Sized {
|
||||
fn default() -> Self;
|
||||
}
|
||||
|
||||
/// Return the default value of a type according to the `Default` trait.
|
||||
///
|
||||
/// The type to return is inferred from context; this is equivalent to
|
||||
/// `Default::default()` but shorter to type.
|
||||
///
|
||||
/// For example:
|
||||
/// ```
|
||||
/// #![feature(default_free_fn)]
|
||||
///
|
||||
/// use std::default::default;
|
||||
///
|
||||
/// #[derive(Default)]
|
||||
/// struct AppConfig {
|
||||
/// foo: FooConfig,
|
||||
/// bar: BarConfig,
|
||||
/// }
|
||||
///
|
||||
/// #[derive(Default)]
|
||||
/// struct FooConfig {
|
||||
/// foo: i32,
|
||||
/// }
|
||||
///
|
||||
/// #[derive(Default)]
|
||||
/// struct BarConfig {
|
||||
/// bar: f32,
|
||||
/// baz: u8,
|
||||
/// }
|
||||
///
|
||||
/// fn main() {
|
||||
/// let options = AppConfig {
|
||||
/// foo: default(),
|
||||
/// bar: BarConfig {
|
||||
/// bar: 10.1,
|
||||
/// ..default()
|
||||
/// },
|
||||
/// };
|
||||
/// }
|
||||
/// ```
|
||||
#[unstable(feature = "default_free_fn", issue = "73014")]
|
||||
#[inline]
|
||||
pub fn default<T: Default>() -> T {
|
||||
Default::default()
|
||||
}
|
||||
|
||||
/// Derive macro generating an impl of the trait `Default`.
|
||||
#[rustc_builtin_macro]
|
||||
#[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
|
||||
|
@ -14,7 +14,16 @@ error[E0425]: cannot find function `default` in this scope
|
||||
--> $DIR/issue-2356.rs:31:5
|
||||
|
|
||||
LL | default();
|
||||
| ^^^^^^^ help: try: `Self::default`
|
||||
| ^^^^^^^
|
||||
|
|
||||
help: try
|
||||
|
|
||||
LL | Self::default();
|
||||
| ^^^^^^^^^^^^^
|
||||
help: consider importing this function
|
||||
|
|
||||
LL | use std::default::default;
|
||||
|
|
||||
|
||||
error[E0425]: cannot find value `whiskers` in this scope
|
||||
--> $DIR/issue-2356.rs:39:5
|
||||
|
Loading…
x
Reference in New Issue
Block a user