diff options
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/path.rs | 2 | ||||
| -rw-r--r-- | src/libcore/vec.rs | 29 |
2 files changed, 14 insertions, 17 deletions
diff --git a/src/libcore/path.rs b/src/libcore/path.rs index 1cfafd25740..5658e57f68a 100644 --- a/src/libcore/path.rs +++ b/src/libcore/path.rs @@ -170,7 +170,7 @@ fn splitext(p: path) -> (str, str) { if vec::len(parts) > 1u { let base = str::connect(vec::init(parts), "."); // We just checked that parts is non-empty, so this is safe - let ext = "." + vec::last_unsafe(parts); + let ext = "." + vec::last(parts); fn is_dotfile(base: str) -> bool { str::is_empty(base) diff --git a/src/libcore/vec.rs b/src/libcore/vec.rs index 3737f9bac1d..f1aac380886 100644 --- a/src/libcore/vec.rs +++ b/src/libcore/vec.rs @@ -187,27 +187,24 @@ fn init<T: copy>(v: [const T]) -> [T] { /* Function: last -Returns the last element of a vector - -Returns: +Returns the last element of a `v`, failing if the vector is empty. -An option containing the last element of `v` if `v` is not empty, or -none if `v` is empty. */ -pure fn last<T: copy>(v: [const T]) -> option<T> { - if len(v) == 0u { ret none; } - ret some(v[len(v) - 1u]); +pure fn last<T: copy>(v: [const T]) -> T { + if len(v) == 0u { fail "last_unsafe: empty vector" } + v[len(v) - 1u] } /* -Function: last_unsafe +Function: last_opt -Returns the last element of a `v`, failing if the vector is empty. +Returns some(x) where `x` is the last element of a vector `v`, +or none if the vector is empty. */ -pure fn last_unsafe<T: copy>(v: [const T]) -> T { - if len(v) == 0u { fail "last_unsafe: empty vector" } - v[len(v) - 1u] +pure fn last_opt<T: copy>(v: [const T]) -> option<T> { + if len(v) == 0u { ret none; } + some(v[len(v) - 1u]) } /* @@ -1270,11 +1267,11 @@ mod tests { #[test] fn test_last() { - let n = last([]); + let n = last_opt([]); assert (n == none); - n = last([1, 2, 3]); + n = last_opt([1, 2, 3]); assert (n == some(3)); - n = last([1, 2, 3, 4, 5]); + n = last_opt([1, 2, 3, 4, 5]); assert (n == some(5)); } |
