mirror of
https://github.com/rust-lang/rust.git
synced 2024-12-25 23:14:12 +00:00
Rollup merge of #44668 - iwillspeak:into-iterator-docs, r=steveklabnik
Add Example of `IntoIterator` as Trait Bound to Docs Part of #44600.
This commit is contained in:
commit
4af3073c71
@ -196,6 +196,23 @@ pub trait FromIterator<A>: Sized {
|
|||||||
/// assert_eq!(i as i32, n);
|
/// assert_eq!(i as i32, n);
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
|
///
|
||||||
|
/// It is common to use `IntoIterator` as a trait bound. This allows
|
||||||
|
/// the input collection type to change, so long as it is still an
|
||||||
|
/// iterator. Additional bounds can be specified by restricting on
|
||||||
|
/// `Item`:
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// fn collect_as_strings<T>(collection: T) -> Vec<String>
|
||||||
|
/// where T: IntoIterator,
|
||||||
|
/// T::Item : std::fmt::Debug,
|
||||||
|
/// {
|
||||||
|
/// collection
|
||||||
|
/// .into_iter()
|
||||||
|
/// .map(|item| format!("{:?}", item))
|
||||||
|
/// .collect()
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
pub trait IntoIterator {
|
pub trait IntoIterator {
|
||||||
/// The type of the elements being iterated over.
|
/// The type of the elements being iterated over.
|
||||||
|
Loading…
Reference in New Issue
Block a user