aboutsummaryrefslogtreecommitdiff
path: root/src/lib/_vec.rs
diff options
context:
space:
mode:
authorPatrick Walton <[email protected]>2011-03-21 17:21:35 -0700
committerPatrick Walton <[email protected]>2011-03-21 17:21:35 -0700
commite56a1720e4dd35f9143fc0620083b406c797fb9c (patch)
tree86f71ac8552e3a43baaa33220cadde96402fd4f1 /src/lib/_vec.rs
parentAdd a binding to ftell() (diff)
downloadrust-e56a1720e4dd35f9143fc0620083b406c797fb9c.tar.xz
rust-e56a1720e4dd35f9143fc0620083b406c797fb9c.zip
Add a "last" function to return the last element of a vector to the standard library
Diffstat (limited to 'src/lib/_vec.rs')
-rw-r--r--src/lib/_vec.rs12
1 files changed, 12 insertions, 0 deletions
diff --git a/src/lib/_vec.rs b/src/lib/_vec.rs
index 1ddb07e3..36b529cc 100644
--- a/src/lib/_vec.rs
+++ b/src/lib/_vec.rs
@@ -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);