Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions library/alloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
#![feature(fmt_internals)]
#![feature(fn_traits)]
#![feature(formatting_options)]
#![feature(from_iter_default)]
#![feature(generic_atomic)]
#![feature(hasher_prefixfree_extras)]
#![feature(inplace_iteration)]
Expand Down
11 changes: 11 additions & 0 deletions library/core/src/iter/traits/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,17 @@ pub trait FromIterator<A>: Sized {
fn from_iter<T: IntoIterator<Item = A>>(iter: T) -> Self;
}

// 为同时实现 Default 和 Extend 的类型实现 FromIterator
#[unstable(feature = "from_iter_default", issue = "58659")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately unstable impl's are not supported (yet?), so you should remove this line and its accompanying feature.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be #[stable(feature = "from_iter_default", since = "CURRENT_RUSTC_VERSION")], and I was probably wrong about removing the feature.

impl<T, A: Default + Extend<T>> FromIterator<T> for A {
#[rustc_diagnostic_item = "from_iter_default"]
default fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
let mut collection = A::default();
collection.extend(iter);
collection
}
}

/// Conversion into an [`Iterator`].
///
/// By implementing `IntoIterator` for a type, you define how it will be
Expand Down
Loading