-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #66577 - WaffleLapkin:iter_take_while_map, r=mark-simul…
…crum Add `Iterator::map_while` In `Iterator` trait there is `*_map` version of [`filter`] — [`filter_map`], however, there is no `*_map` version of [`take_while`], that can also be useful. ### Use cases In my code, I've found that I need to iterate through iterator of `Option`s, stopping on the first `None`. So I've written code like this: ```rust let arr = [Some(4), Some(10), None, Some(3)]; let mut iter = arr.iter() .take_while(|x| x.is_some()) .map(|x| x.unwrap()); assert_eq!(iter.next(), Some(4)); assert_eq!(iter.next(), Some(10)); assert_eq!(iter.next(), None); assert_eq!(iter.next(), None); ``` Thit code 1) isn't clean 2) In theory, can generate bad bytecode (I'm actually **not** sure, but I think that `unwrap` would generate additional branches with `panic!`) The same code, but with `map_while` (in the original PR message it was named "take_while_map"): ```rust let arr = [Some(4), Some(10), None, Some(3)]; let mut iter = arr.iter().map_while(std::convert::identity); ``` Also, `map_while` can be useful when converting something (as in [examples]). [`filter`]: https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.filter [`filter_map`]: https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.filter_map [`take_while`]: https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.take_while [examples]: https://github.com/rust-lang/rust/compare/master...WaffleLapkin:iter_take_while_map?expand=1#diff-7e57917f962fe6ffdfba51e4955ad6acR1042
- Loading branch information
Showing
5 changed files
with
195 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters