Skip to content

Commit e56a172

Browse files
committed
Add a "last" function to return the last element of a vector to the standard library
1 parent 71de17d commit e56a172

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/lib/_vec.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import option.none;
2+
import option.some;
3+
14
import vbuf = rustrt.vbuf;
25

36
type operator2[T,U,V] = fn(&T, &U) -> V;
@@ -115,6 +118,15 @@ fn print_debug_info[T](vec[mutable? T] v) {
115118
rustrt.vec_print_debug_info[T](v);
116119
}
117120

121+
// Returns the last element of v.
122+
fn last[T](vec[mutable? T] v) -> option.t[T] {
123+
auto l = len[T](v);
124+
if (l == 0u) {
125+
ret none[T];
126+
}
127+
ret some[T](v.(l - 1u));
128+
}
129+
118130
// Returns elements from [start..end) from v.
119131
fn slice[T](vec[mutable? T] v, uint start, uint end) -> vec[T] {
120132
check (start <= end);

0 commit comments

Comments
 (0)