diff --git a/src/libcollections/vec.rs b/src/libcollections/vec.rs index a0c92887c43ed..6618906cf69de 100644 --- a/src/libcollections/vec.rs +++ b/src/libcollections/vec.rs @@ -964,7 +964,7 @@ impl Vec { #[inline] pub fn swap_remove(&mut self, index: uint) -> Option { let length = self.len(); - if index < length - 1 { + if length > 0 && index < length - 1 { self.as_mut_slice().swap(index, length - 1); } else if index >= length { return None @@ -2003,6 +2003,12 @@ mod tests { let _ = vec[3]; } + #[test] + fn test_swap_remove_empty() { + let mut vec: Vec = vec!(); + assert_eq!(vec.swap_remove(0), None); + } + #[bench] fn bench_new(b: &mut Bencher) { b.iter(|| {