Skip to content

Commit 4af3073

Browse files
authored
Rollup merge of rust-lang#44668 - iwillspeak:into-iterator-docs, r=steveklabnik
Add Example of `IntoIterator` as Trait Bound to Docs Part of rust-lang#44600.
2 parents 3bbe153 + ebd0e4f commit 4af3073

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/libcore/iter/traits.rs

+17
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,23 @@ pub trait FromIterator<A>: Sized {
196196
/// assert_eq!(i as i32, n);
197197
/// }
198198
/// ```
199+
///
200+
/// It is common to use `IntoIterator` as a trait bound. This allows
201+
/// the input collection type to change, so long as it is still an
202+
/// iterator. Additional bounds can be specified by restricting on
203+
/// `Item`:
204+
///
205+
/// ```rust
206+
/// fn collect_as_strings<T>(collection: T) -> Vec<String>
207+
/// where T: IntoIterator,
208+
/// T::Item : std::fmt::Debug,
209+
/// {
210+
/// collection
211+
/// .into_iter()
212+
/// .map(|item| format!("{:?}", item))
213+
/// .collect()
214+
/// }
215+
/// ```
199216
#[stable(feature = "rust1", since = "1.0.0")]
200217
pub trait IntoIterator {
201218
/// The type of the elements being iterated over.

0 commit comments

Comments
 (0)