From f39384a976d6abe2bd910082453c0c514c77a4d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adolfo=20Ochagav=C3=ADa?= Date: Sun, 15 Jun 2014 11:12:20 +0200 Subject: [PATCH] Add for_each function to Iterator trait --- src/libcore/iter.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/libcore/iter.rs b/src/libcore/iter.rs index 32c47d36bed99..be42701eff91f 100644 --- a/src/libcore/iter.rs +++ b/src/libcore/iter.rs @@ -448,6 +448,19 @@ pub trait Iterator { } } + /// Applies the specified function to each element yielded by the iterator + /// + /// # Example + /// + /// ```rust + /// let a = [1, 2, 3]; + /// a.iter().for_each(|x| println!("{}", x)); + /// ``` + #[inline] + fn for_each(&mut self, f: |A|) { + for x in *self { f(x); } + } + /// Loops through the entire iterator, collecting all of the elements into /// a container implementing `FromIterator`. ///