@@ -212,7 +212,7 @@ pub pure fn build_sized_opt<A>(size: Option<uint>,
212212
213213/// Returns the first element of a vector
214214pub pure fn head < T > ( v : & r/[ T ] ) -> & r /T {
215- if v. len ( ) == 0 { fail ! ( ~"last_unsafe : empty vector") }
215+ if v. len ( ) == 0 { fail ! ( ~"head : empty vector") }
216216 &v[0]
217217}
218218
@@ -237,18 +237,15 @@ pub pure fn initn<T>(v: &r/[T], n: uint) -> &r/[T] {
237237}
238238
239239/// Returns the last element of the slice `v`, failing if the slice is empty.
240- pub pure fn last<T:Copy >(v: &[const T]) -> T {
241- if len(v ) == 0u { fail!(~" last_unsafe : empty vector") }
242- v[ len(v ) - 1u ]
240+ pub pure fn last<T>(v: &r/[ T]) -> &r/ T {
241+ if v. len() == 0 { fail!(~" last : empty vector") }
242+ &v[v. len() - 1 ]
243243}
244244
245- /**
246- * Returns `Some(x)` where `x` is the last element of the slice `v`,
247- * or `none` if the vector is empty.
248- */
249- pub pure fn last_opt<T:Copy>(v: &[const T]) -> Option<T> {
250- if len(v) == 0u { return None; }
251- Some(v[len(v) - 1u])
245+ /// Returns `Some(x)` where `x` is the last element of the slice `v`, or
246+ /// `None` if the vector is empty.
247+ pub pure fn last_opt<T>(v: &r/[T]) -> Option<&r/T> {
248+ if v.len() == 0 { None } else { Some(&v[v.len() - 1]) }
252249}
253250
254251/// Return a slice that points into another slice.
@@ -1696,16 +1693,11 @@ impl<T> Container for &[const T] {
16961693}
16971694
16981695pub trait CopyableVector < T > {
1699- pure fn last ( & self ) -> T ;
17001696 pure fn slice ( & self , start : uint , end : uint ) -> ~[ T ] ;
17011697}
17021698
17031699/// Extension methods for vectors
17041700impl < T : Copy > CopyableVector < T > for & [ const T ] {
1705- /// Returns the last element of a `v`, failing if the vector is empty.
1706- #[ inline]
1707- pure fn last ( & self ) -> T { last ( * self ) }
1708-
17091701 /// Returns a copy of the elements from [`start`..`end`) from `v`.
17101702 #[ inline]
17111703 pure fn slice ( & self , start : uint , end : uint ) -> ~[ T ] {
@@ -1721,6 +1713,8 @@ pub trait ImmutableVector<T> {
17211713 pure fn tailn ( & self , n : uint ) -> & self /[ T ] ;
17221714 pure fn init ( & self ) -> & self /[ T ] ;
17231715 pure fn initn ( & self , n : uint ) -> & self /[ T ] ;
1716+ pure fn last ( & self ) -> & self /T ;
1717+ pure fn last_opt ( & self ) -> Option < & self /T > ;
17241718 pure fn foldr < U : Copy > ( & self , z : U , p : fn ( t : & T , u : U ) -> U ) -> U ;
17251719 pure fn map < U > ( & self , f : fn ( t : & T ) -> U ) -> ~[ U ] ;
17261720 pure fn mapi < U > ( & self , f : fn ( uint , t : & T ) -> U ) -> ~[ U ] ;
@@ -1762,6 +1756,14 @@ impl<T> ImmutableVector<T> for &[T] {
17621756 #[ inline]
17631757 pure fn initn ( & self , n : uint ) -> & self /[ T ] { initn ( * self , n) }
17641758
1759+ /// Returns the last element of a `v`, failing if the vector is empty.
1760+ #[ inline]
1761+ pure fn last ( & self ) -> & self /T { last ( * self ) }
1762+
1763+ /// Returns the last element of a `v`, failing if the vector is empty.
1764+ #[ inline]
1765+ pure fn last_opt ( & self ) -> Option < & self /T > { last_opt ( * self ) }
1766+
17651767 /// Reduce a vector from right to left
17661768 #[ inline]
17671769 pure fn foldr < U : Copy > ( & self , z : U , p : fn ( t : & T , u : U ) -> U ) -> U {
@@ -2679,12 +2681,27 @@ mod tests {
26792681
26802682 #[ test]
26812683 fn test_last ( ) {
2682- let mut n = last_opt ( ~[ ] ) ;
2683- assert ( n. is_none ( ) ) ;
2684- n = last_opt ( ~[ 1 , 2 , 3 ] ) ;
2685- assert ( n == Some ( 3 ) ) ;
2686- n = last_opt ( ~[ 1 , 2 , 3 , 4 , 5 ] ) ;
2687- assert ( n == Some ( 5 ) ) ;
2684+ let mut a = ~[ 11 ] ;
2685+ assert a. last ( ) == & 11 ;
2686+ a = ~[ 11 , 12 ] ;
2687+ assert a. last ( ) == & 12 ;
2688+ }
2689+
2690+ #[ test]
2691+ #[ should_fail]
2692+ fn test_last_empty ( ) {
2693+ let a: ~[ int ] = ~[ ] ;
2694+ a. last ( ) ;
2695+ }
2696+
2697+ #[ test]
2698+ fn test_last_opt ( ) {
2699+ let mut a = ~[ ] ;
2700+ assert a. last_opt ( ) == None ;
2701+ a = ~[ 11 ] ;
2702+ assert a. last_opt ( ) . unwrap ( ) == & 11 ;
2703+ a = ~[ 11 , 12 ] ;
2704+ assert a. last_opt ( ) . unwrap ( ) == & 12 ;
26882705 }
26892706
26902707 #[ test]
0 commit comments