mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-23 02:27:39 +03:00
Auto merge of #42569 - birkenfeld:patch-2, r=frewsxcv
Simplify FromIterator example of Result The previous version may be clearer for newcomers, but this is how you'd write it idiomaticly.
This commit is contained in:
@@ -1060,12 +1060,9 @@ impl<A, E, V: FromIterator<A>> FromIterator<Result<A, E>> for Result<V, E> {
|
||||
/// checking for overflow:
|
||||
///
|
||||
/// ```
|
||||
/// use std::u32;
|
||||
///
|
||||
/// let v = vec![1, 2];
|
||||
/// let res: Result<Vec<u32>, &'static str> = v.iter().map(|&x: &u32|
|
||||
/// if x == u32::MAX { Err("Overflow!") }
|
||||
/// else { Ok(x + 1) }
|
||||
/// let res: Result<Vec<u32>, &'static str> = v.iter().map(|x: &u32|
|
||||
/// x.checked_add(1).ok_or("Overflow!")
|
||||
/// ).collect();
|
||||
/// assert!(res == Ok(vec![2, 3]));
|
||||
/// ```
|
||||
@@ -1126,4 +1123,4 @@ fn from_ok(v: T) -> Self {
|
||||
fn from_error(v: E) -> Self {
|
||||
Err(v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user