Skip to content

Commit 310ec44

Browse files
committed
---
yaml --- r: 95367 b: refs/heads/dist-snap c: 91abfd4 h: refs/heads/master i: 95365: 6634749 95363: cdc4d23 95359: 15a88d7 v: v3
1 parent 17066e3 commit 310ec44

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
@@ -6,7 +6,7 @@ refs/heads/try: c274a6888410ce3e357e014568b43310ed787d36
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: 147ecfdd8221e4a4d4e090486829a06da1e0ca3c
9-
refs/heads/dist-snap: e88064d18f681cc702baebeb8951d5dc6ed10fb9
9+
refs/heads/dist-snap: 91abfd425d74af4cd083c3a6fc15bb39a4aa33da
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503
1212
refs/heads/try3: 9387340aab40a73e8424c48fd42f0c521a4875c0

branches/dist-snap/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)