diff options
| author | bors <bors@rust-lang.org> | 2020-06-08 16:32:49 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2020-06-08 16:32:49 +0000 |
| commit | bc10b68e798477066d4b1ec4886a3b1cdc4feb7e (patch) | |
| tree | 1c2d58cb9bd0d9a45bc5972c256eac6db0f94a14 /src/libcore | |
| parent | 73558160933b2764ed9a84b1b2b647e128eac3f8 (diff) | |
| parent | 7983e56f40e7536a645993485754c302e5090435 (diff) | |
| download | rust-bc10b68e798477066d4b1ec4886a3b1cdc4feb7e.tar.gz rust-bc10b68e798477066d4b1ec4886a3b1cdc4feb7e.zip | |
Auto merge of #73115 - RalfJung:rollup-jecowhz, r=RalfJung
Rollup of 10 pull requests Successful merges: - #72026 (Update annotate-snippets-rs to 0.8.0) - #72583 (impl AsRef<[T]> for vec::IntoIter<T>) - #72615 (Fix documentation example for gcov profiling) - #72761 (Added the documentation for the 'use' keyword) - #72799 (Add `-Z span-debug` to allow for easier debugging of proc macros) - #72811 (Liballoc impl) - #72963 (Cstring `from_raw` and `into_raw` safety precisions) - #73001 (Free `default()` forwarding to `Default::default()`) - #73075 (Add comments to `Resolve::get_module`) - #73092 (Clean up E0646) Failed merges: r? @ghost
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/default.rs | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/libcore/default.rs b/src/libcore/default.rs index 06402a05d26..9a8d65cd4e0 100644 --- a/src/libcore/default.rs +++ b/src/libcore/default.rs @@ -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")] |
