Skip to content

Commit d363d56

Browse files
committed
---
yaml --- r: 145907 b: refs/heads/try2 c: 91abfd4 h: refs/heads/master i: 145905: f28c2b0 145903: a4a91df v: v3
1 parent 01e9bc4 commit d363d56

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: e88064d18f681cc702baebeb8951d5dc6ed10fb9
8+
refs/heads/try2: 91abfd425d74af4cd083c3a6fc15bb39a4aa33da
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/src/libstd/vec.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,7 @@ pub trait ImmutableVector<'self, T> {
840840
fn window_iter(self, size: uint) -> WindowIter<'self, T>;
841841
fn chunk_iter(self, size: uint) -> ChunkIter<'self, T>;
842842

843+
fn get_opt(&self, index: uint) -> Option<&'self T>;
843844
fn head(&self) -> &'self T;
844845
fn head_opt(&self) -> Option<&'self T>;
845846
fn tail(&self) -> &'self [T];
@@ -1019,6 +1020,13 @@ impl<'self,T> ImmutableVector<'self, T> for &'self [T] {
10191020
ChunkIter { v: self, size: size }
10201021
}
10211022

1023+
/// Returns the element of a vector at the given index, or `None` if the
1024+
/// index is out of bounds
1025+
#[inline]
1026+
fn get_opt(&self, index: uint) -> Option<&'self T> {
1027+
if index < self.len() { Some(&self[index]) } else { None }
1028+
}
1029+
10221030
/// Returns the first element of a vector, failing if the vector is empty.
10231031
#[inline]
10241032
fn head(&self) -> &'self T {
@@ -2574,6 +2582,16 @@ mod tests {
25742582
assert_eq!(v2.len(), 2);
25752583
}
25762584

2585+
#[test]
2586+
fn test_get_opt() {
2587+
let mut a = ~[11];
2588+
assert_eq!(a.get_opt(1), None);
2589+
a = ~[11, 12];
2590+
assert_eq!(a.get_opt(1).unwrap(), &12);
2591+
a = ~[11, 12, 13];
2592+
assert_eq!(a.get_opt(1).unwrap(), &12);
2593+
}
2594+
25772595
#[test]
25782596
fn test_head() {
25792597
let mut a = ~[11];

0 commit comments

Comments
 (0)