diff options
| author | Huon Wilson <dbau.pp+github@gmail.com> | 2014-02-23 10:56:38 +1100 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2014-02-24 21:22:26 -0800 |
| commit | 16e635cdfbb6b041886d1bccd28fa5e7e34c9f47 (patch) | |
| tree | eae398f604ca7c7798707ec1fd935fef79a0a493 /src/libstd/vec.rs | |
| parent | 3ca01676bcbd092b04608cc0eee843b7031e46cb (diff) | |
| download | rust-16e635cdfbb6b041886d1bccd28fa5e7e34c9f47.tar.gz rust-16e635cdfbb6b041886d1bccd28fa5e7e34c9f47.zip | |
std: make .swap_remove return Option<T>.
This is one of the last raw "indexing" method on vectors that returns `T` instead of the Option.
Diffstat (limited to 'src/libstd/vec.rs')
| -rw-r--r-- | src/libstd/vec.rs | 50 |
1 files changed, 30 insertions, 20 deletions
diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index d8cb8bf3ed1..cf49ea53562 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -1368,13 +1368,24 @@ pub trait OwnedVector<T> { /// ``` fn remove(&mut self, i: uint) -> Option<T>; - /** - * Remove an element from anywhere in the vector and return it, replacing it - * with the last element. This does not preserve ordering, but is O(1). - * - * Fails if index >= length. - */ - fn swap_remove(&mut self, index: uint) -> T; + /// Remove an element from anywhere in the vector and return it, replacing it + /// with the last element. This does not preserve ordering, but is O(1). + /// + /// Returns `None` if `index` is out of bounds. + /// + /// # Example + /// ```rust + /// let mut v = ~[~"foo", ~"bar", ~"baz", ~"qux"]; + /// + /// assert_eq!(v.swap_remove(1), Some(~"bar")); + /// assert_eq!(v, ~[~"foo", ~"qux", ~"baz"]); + /// + /// assert_eq!(v.swap_remove(0), Some(~"foo")); + /// assert_eq!(v, ~[~"baz", ~"qux"]); + /// + /// assert_eq!(v.swap_remove(2), None); + /// ``` + fn swap_remove(&mut self, index: uint) -> Option<T>; /// Shorten a vector, dropping excess elements. fn truncate(&mut self, newlen: uint); @@ -1580,15 +1591,14 @@ impl<T> OwnedVector<T> for ~[T] { None } } - fn swap_remove(&mut self, index: uint) -> T { + fn swap_remove(&mut self, index: uint) -> Option<T> { let ln = self.len(); - if index >= ln { - fail!("vec::swap_remove - index {} >= length {}", index, ln); - } if index < ln - 1 { self.swap(index, ln - 1); + } else if index >= ln { + return None } - self.pop().unwrap() + self.pop() } fn truncate(&mut self, newlen: uint) { let oldlen = self.len(); @@ -3194,15 +3204,15 @@ mod tests { fn test_swap_remove() { let mut v = ~[1, 2, 3, 4, 5]; let mut e = v.swap_remove(0); - assert_eq!(v.len(), 4); - assert_eq!(e, 1); - assert_eq!(v[0], 5); + assert_eq!(e, Some(1)); + assert_eq!(v, ~[5, 2, 3, 4]); e = v.swap_remove(3); - assert_eq!(v.len(), 3); - assert_eq!(e, 4); - assert_eq!(v[0], 5); - assert_eq!(v[1], 2); - assert_eq!(v[2], 3); + assert_eq!(e, Some(4)); + assert_eq!(v, ~[5, 2, 3]); + + e = v.swap_remove(3); + assert_eq!(e, None); + assert_eq!(v, ~[5, 2, 3]); } #[test] |
