Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Demo FromIterator short-circuiting #59362

Merged
merged 4 commits into from
Mar 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 20 additions & 0 deletions src/libcore/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1315,6 +1315,26 @@ impl<A, V: FromIterator<A>> FromIterator<Option<A>> for Option<V> {
/// Since the last element is zero, it would underflow. Thus, the resulting
/// value is `None`.
///
/// Here is a variation on the previous example, showing that no
/// further elements are taken from `iter` after the first `None`.
///
/// ```
/// let items = vec![3_u16, 2, 1, 10];
Copy link
Contributor

@DevQps DevQps Mar 25, 2019

Choose a reason for hiding this comment

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

Just as a question from a relative newbie: What is this 3_u16 syntax? I have seen the 3u16 syntax but never with the underscore. Is there a difference between this syntax?

I would suggest that using 3u16 or even let items: Vec<u16>= vec![3, 2, 1, 10]; to make it more clear for people that are not too experienced with Rust. But that is just my humble opinion!

EDIT: Nice examples by the way! I never knew it worked this way :)

Copy link
Contributor

Choose a reason for hiding this comment

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

The _ here is merely an effect of separators in numeric literals, e.g. 3_000_000_u32 or 3_000____u32. The underscores are for readability and have no effect otherwise. 3u16 and 3_u16 are thus equivalent.

Copy link
Contributor

Choose a reason for hiding this comment

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

Alright! Thanks for sharing! :)

///
/// let mut shared = 0;
///
/// let res: Option<Vec<u16>> = items
/// .iter()
/// .map(|x| { shared += x; x.checked_sub(2) })
/// .collect();
///
/// assert_eq!(res, None);
/// assert_eq!(shared, 6);
/// ```
///
/// Since the third element caused an underflow, no further elements were taken,
/// so the final value of `shared` is 6 (= `3 + 2 + 1`), not 16.
///
/// [`Iterator`]: ../iter/trait.Iterator.html
#[inline]
fn from_iter<I: IntoIterator<Item=Option<A>>>(iter: I) -> Option<V> {
Expand Down
28 changes: 28 additions & 0 deletions src/libcore/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1202,6 +1202,34 @@ impl<A, E, V: FromIterator<A>> FromIterator<Result<A, E>> for Result<V, E> {
/// ).collect();
/// assert_eq!(res, Ok(vec![2, 3]));
/// ```
///
/// Here is another example that tries to subtract one from another list
/// of integers, this time checking for underflow:
///
/// ```
/// let v = vec![1, 2, 0];
/// let res: Result<Vec<u32>, &'static str> = v.iter().map(|x: &u32|
/// x.checked_sub(1).ok_or("Underflow!")
/// ).collect();
/// assert_eq!(res, Err("Underflow!"));
/// ```
///
/// Here is a variation on the previous example, showing that no
/// further elements are taken from `iter` after the first `Err`.
///
/// ```
/// let v = vec![3, 2, 1, 10];
/// let mut shared = 0;
/// let res: Result<Vec<u32>, &'static str> = v.iter().map(|x: &u32| {
/// shared += x;
/// x.checked_sub(2).ok_or("Underflow!")
/// }).collect();
/// assert_eq!(res, Err("Underflow!"));
/// assert_eq!(shared, 6);
/// ```
///
/// Since the third element caused an underflow, no further elements were taken,
/// so the final value of `shared` is 6 (= `3 + 2 + 1`), not 16.
#[inline]
fn from_iter<I: IntoIterator<Item=Result<A, E>>>(iter: I) -> Result<V, E> {
// FIXME(#11084): This could be replaced with Iterator::scan when this
Expand Down