Add a "last" function to return the last element of a vector to the standard library
This commit is contained in:
parent
71de17d38e
commit
e56a1720e4
@ -1,3 +1,6 @@
|
||||
import option.none;
|
||||
import option.some;
|
||||
|
||||
import vbuf = rustrt.vbuf;
|
||||
|
||||
type operator2[T,U,V] = fn(&T, &U) -> V;
|
||||
@ -115,6 +118,15 @@ fn print_debug_info[T](vec[mutable? T] v) {
|
||||
rustrt.vec_print_debug_info[T](v);
|
||||
}
|
||||
|
||||
// Returns the last element of v.
|
||||
fn last[T](vec[mutable? T] v) -> option.t[T] {
|
||||
auto l = len[T](v);
|
||||
if (l == 0u) {
|
||||
ret none[T];
|
||||
}
|
||||
ret some[T](v.(l - 1u));
|
||||
}
|
||||
|
||||
// Returns elements from [start..end) from v.
|
||||
fn slice[T](vec[mutable? T] v, uint start, uint end) -> vec[T] {
|
||||
check (start <= end);
|
||||
|
Loading…
x
Reference in New Issue
Block a user