Skip to content

Commit b35d1a8

Browse files
committed
Implement Index and IndexMut for Vec
1 parent 06c7ee9 commit b35d1a8

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/libcollections/vec.rs

+28
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,21 @@ impl<T:Clone> Clone for Vec<T> {
363363
}
364364
}
365365

366+
impl<T> Index<uint,T> for Vec<T> {
367+
#[inline]
368+
fn index<'a>(&'a self, index: &uint) -> &'a T {
369+
self.get(*index)
370+
}
371+
}
372+
373+
// FIXME(#12825) Indexing will always try IndexMut first and that causes issues.
374+
/*impl<T> IndexMut<uint,T> for Vec<T> {
375+
#[inline]
376+
fn index_mut<'a>(&'a mut self, index: &uint) -> &'a mut T {
377+
self.get_mut(*index)
378+
}
379+
}*/
380+
366381
impl<T> FromIterator<T> for Vec<T> {
367382
#[inline]
368383
fn from_iter<I:Iterator<T>>(mut iterator: I) -> Vec<T> {
@@ -1847,6 +1862,19 @@ mod tests {
18471862
v.truncate(0);
18481863
}
18491864

1865+
#[test]
1866+
fn test_index() {
1867+
let vec = vec!(1i, 2, 3);
1868+
assert!(vec[1] == 2);
1869+
}
1870+
1871+
#[test]
1872+
#[should_fail]
1873+
fn test_index_out_of_bounds() {
1874+
let vec = vec!(1i, 2, 3);
1875+
let _ = vec[3];
1876+
}
1877+
18501878
#[bench]
18511879
fn bench_new(b: &mut Bencher) {
18521880
b.iter(|| {

0 commit comments

Comments
 (0)