Skip to content

Commit 410d70b

Browse files
committed
auto merge of #14992 : nathantypanski/rust/collect-docs, r=huonw
This updates the documentation for result::collect() and option::collect() to use the new-style syntax for owned pointers and vectors. closes #14991
2 parents d6736a1 + feceb12 commit 410d70b

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

src/libcore/option.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -574,13 +574,17 @@ impl<A> ExactSize<A> for Item<A> {}
574574
/// Here is an example which increments every integer in a vector,
575575
/// checking for overflow:
576576
///
577-
/// fn inc_conditionally(x: uint) -> Option<uint> {
578-
/// if x == uint::MAX { return None; }
579-
/// else { return Some(x+1u); }
580-
/// }
581-
/// let v = [1u, 2, 3];
582-
/// let res = collect(v.iter().map(|&x| inc_conditionally(x)));
583-
/// assert!(res == Some(~[2u, 3, 4]));
577+
/// ```rust
578+
/// use std::option;
579+
/// use std::uint;
580+
///
581+
/// let v = vec!(1u, 2u);
582+
/// let res: Option<Vec<uint>> = option::collect(v.iter().map(|x: &uint|
583+
/// if *x == uint::MAX { None }
584+
/// else { Some(x + 1) }
585+
/// ));
586+
/// assert!(res == Some(vec!(2u, 3u)));
587+
/// ```
584588
#[inline]
585589
pub fn collect<T, Iter: Iterator<Option<T>>, V: FromIterator<T>>(iter: Iter) -> Option<V> {
586590
// FIXME(#11084): This should be twice as fast once this bug is closed.

src/libcore/result.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -572,13 +572,17 @@ impl<T: Show, E> Result<T, E> {
572572
/// Here is an example which increments every integer in a vector,
573573
/// checking for overflow:
574574
///
575-
/// fn inc_conditionally(x: uint) -> Result<uint, &'static str> {
576-
/// if x == uint::MAX { return Err("overflow"); }
577-
/// else { return Ok(x+1u); }
578-
/// }
579-
/// let v = [1u, 2, 3];
580-
/// let res = collect(v.iter().map(|&x| inc_conditionally(x)));
581-
/// assert!(res == Ok(~[2u, 3, 4]));
575+
/// ```rust
576+
/// use std::result;
577+
/// use std::uint;
578+
///
579+
/// let v = vec!(1u, 2u);
580+
/// let res: Result<Vec<uint>, &'static str> = result::collect(v.iter().map(|x: &uint|
581+
/// if *x == uint::MAX { Err("Overflow!") }
582+
/// else { Ok(x + 1) }
583+
/// ));
584+
/// assert!(res == Ok(vec!(2u, 3u)));
585+
/// ```
582586
#[inline]
583587
pub fn collect<T, E, Iter: Iterator<Result<T, E>>, V: FromIterator<T>>(iter: Iter) -> Result<V, E> {
584588
// FIXME(#11084): This should be twice as fast once this bug is closed.

0 commit comments

Comments
 (0)