Skip to content

Commit

Permalink
change ~[] -> Vec for collect()
Browse files Browse the repository at this point in the history
This updates the documentation for result::collect() and
option::collect() to use the new-style syntax for vectors, instead of
the old ~[].

Also updates the code blocks for these docs so they will be tested
automatically.

closes #14991
  • Loading branch information
nathantypanski committed Jun 18, 2014
1 parent 1bb42e5 commit feceb12
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
18 changes: 11 additions & 7 deletions src/libcore/option.rs
Expand Up @@ -574,13 +574,17 @@ impl<A> ExactSize<A> for Item<A> {}
/// Here is an example which increments every integer in a vector,
/// checking for overflow:
///
/// fn inc_conditionally(x: uint) -> Option<uint> {
/// if x == uint::MAX { return None; }
/// else { return Some(x+1u); }
/// }
/// let v = [1u, 2, 3];
/// let res = collect(v.iter().map(|&x| inc_conditionally(x)));
/// assert!(res == Some(~[2u, 3, 4]));
/// ```rust
/// use std::option;
/// use std::uint;
///
/// let v = vec!(1u, 2u);
/// let res: Option<Vec<uint>> = option::collect(v.iter().map(|x: &uint|
/// if *x == uint::MAX { None }
/// else { Some(x + 1) }
/// ));
/// assert!(res == Some(vec!(2u, 3u)));
/// ```
#[inline]
pub fn collect<T, Iter: Iterator<Option<T>>, V: FromIterator<T>>(iter: Iter) -> Option<V> {
// FIXME(#11084): This should be twice as fast once this bug is closed.
Expand Down
18 changes: 11 additions & 7 deletions src/libcore/result.rs
Expand Up @@ -572,13 +572,17 @@ impl<T: Show, E> Result<T, E> {
/// Here is an example which increments every integer in a vector,
/// checking for overflow:
///
/// fn inc_conditionally(x: uint) -> Result<uint, &'static str> {
/// if x == uint::MAX { return Err("overflow"); }
/// else { return Ok(x+1u); }
/// }
/// let v = [1u, 2, 3];
/// let res = collect(v.iter().map(|&x| inc_conditionally(x)));
/// assert!(res == Ok(~[2u, 3, 4]));
/// ```rust
/// use std::result;
/// use std::uint;
///
/// let v = vec!(1u, 2u);
/// let res: Result<Vec<uint>, &'static str> = result::collect(v.iter().map(|x: &uint|
/// if *x == uint::MAX { Err("Overflow!") }
/// else { Ok(x + 1) }
/// ));
/// assert!(res == Ok(vec!(2u, 3u)));
/// ```
#[inline]
pub fn collect<T, E, Iter: Iterator<Result<T, E>>, V: FromIterator<T>>(iter: Iter) -> Result<V, E> {
// FIXME(#11084): This should be twice as fast once this bug is closed.
Expand Down

5 comments on commit feceb12

@bors
Copy link
Contributor

@bors bors commented on feceb12 Jun 18, 2014

Choose a reason for hiding this comment

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

saw approval from huonw
at nathantypanski@feceb12

@bors
Copy link
Contributor

@bors bors commented on feceb12 Jun 18, 2014

Choose a reason for hiding this comment

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

merging nathantypanski/rust/collect-docs = feceb12 into auto

@bors
Copy link
Contributor

@bors bors commented on feceb12 Jun 18, 2014

Choose a reason for hiding this comment

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

nathantypanski/rust/collect-docs = feceb12 merged ok, testing candidate = 410d70b

@bors
Copy link
Contributor

@bors bors commented on feceb12 Jun 18, 2014

Choose a reason for hiding this comment

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

fast-forwarding master to auto = 410d70b

Please sign in to comment.