@@ -238,44 +238,6 @@ pub fn build_sized_opt<A>(size: Option<uint>,
238238
239239// Accessors
240240
241- /// Returns the first element of a vector
242- pub fn head < ' r , T > ( v : & ' r [ T ] ) -> & ' r T {
243- if v. len ( ) == 0 { fail ! ( "head: empty vector" ) }
244- & v[ 0 ]
245- }
246-
247- /// Returns `Some(x)` where `x` is the first element of the slice `v`,
248- /// or `None` if the vector is empty.
249- pub fn head_opt < ' r , T > ( v : & ' r [ T ] ) -> Option < & ' r T > {
250- if v. len ( ) == 0 { None } else { Some ( & v[ 0 ] ) }
251- }
252-
253- /// Returns a vector containing all but the first element of a slice
254- pub fn tail < ' r , T > ( v : & ' r [ T ] ) -> & ' r [ T ] { v. slice ( 1 , v. len ( ) ) }
255-
256- /// Returns a vector containing all but the first `n` elements of a slice
257- pub fn tailn < ' r , T > ( v : & ' r [ T ] , n : uint ) -> & ' r [ T ] { v. slice ( n, v. len ( ) ) }
258-
259- /// Returns a vector containing all but the last element of a slice
260- pub fn init < ' r , T > ( v : & ' r [ T ] ) -> & ' r [ T ] { v. slice ( 0 , v. len ( ) - 1 ) }
261-
262- /// Returns a vector containing all but the last `n' elements of a slice
263- pub fn initn < ' r , T > ( v : & ' r [ T ] , n : uint ) -> & ' r [ T ] {
264- v. slice ( 0 , v. len ( ) - n)
265- }
266-
267- /// Returns the last element of the slice `v`, failing if the slice is empty.
268- pub fn last < ' r , T > ( v : & ' r [ T ] ) -> & ' r T {
269- if v. len ( ) == 0 { fail ! ( "last: empty vector" ) }
270- & v[ v. len ( ) - 1 ]
271- }
272-
273- /// Returns `Some(x)` where `x` is the last element of the slice `v`, or
274- /// `None` if the vector is empty.
275- pub fn last_opt < ' r , T > ( v : & ' r [ T ] ) -> Option < & ' r T > {
276- if v. len ( ) == 0 { None } else { Some ( & v[ v. len ( ) - 1 ] ) }
277- }
278-
279241/// Copies
280242
281243/// Split the vector `v` by applying each element against the predicate `f`.
@@ -1678,35 +1640,49 @@ impl<'self,T> ImmutableVector<'self, T> for &'self [T] {
16781640
16791641 /// Returns the first element of a vector, failing if the vector is empty.
16801642 #[ inline]
1681- fn head( & self ) -> & ' self T { head( * self ) }
1643+ fn head( & self ) -> & ' self T {
1644+ if self. len( ) == 0 { fail ! ( "head: empty vector" ) }
1645+ & self [ 0 ]
1646+ }
16821647
1683- /// Returns the first element of a vector
1648+ /// Returns the first element of a vector, or `None` if it is empty
16841649 #[ inline]
1685- fn head_opt( & self ) -> Option < & ' self T > { head_opt( * self ) }
1650+ fn head_opt ( & self ) -> Option < & ' self T > {
1651+ if self . len( ) == 0 { None } else { Some ( & self [ 0 ] ) }
1652+ }
16861653
16871654 /// Returns all but the first element of a vector
16881655 #[ inline]
1689- fn tail( & self ) -> & ' self [ T ] { tail ( * self ) }
1656+ fn tail( & self ) -> & ' self [ T ] { self . slice ( 1 , self . len ( ) ) }
16901657
16911658 /// Returns all but the first `n' elements of a vector
16921659 #[ inline]
1693- fn tailn( & self , n: uint) -> & ' self [ T ] { tailn ( * self , n ) }
1660+ fn tailn( & self , n: uint) -> & ' self [ T ] { self . slice ( n , self . len ( ) ) }
16941661
1695- /// Returns all but the last elemnt of a vector
1662+ /// Returns all but the last element of a vector
16961663 #[ inline]
1697- fn init( & self ) -> & ' self [ T ] { init( * self ) }
1664+ fn init( & self ) -> & ' self [ T ] {
1665+ self . slice( 0 , self . len( ) - 1 )
1666+ }
16981667
16991668 /// Returns all but the last `n' elemnts of a vector
17001669 #[ inline]
1701- fn initn( & self , n: uint) -> & ' self [ T ] { initn( * self , n) }
1670+ fn initn( & self , n: uint) -> & ' self [ T ] {
1671+ self . slice( 0 , self . len( ) - n)
1672+ }
17021673
1703- /// Returns the last element of a `v` , failing if the vector is empty.
1674+ /// Returns the last element of a vector , failing if the vector is empty.
17041675 #[ inline]
1705- fn last( & self ) -> & ' self T { last( * self ) }
1676+ fn last( & self ) -> & ' self T {
1677+ if self. len( ) == 0 { fail ! ( "last: empty vector" ) }
1678+ & self [ self . len( ) - 1 ]
1679+ }
17061680
1707- /// Returns the last element of a `v`, failing if the vector is empty.
1681+ /// Returns the last element of a vector, or `None` if it is empty.
17081682 #[ inline]
1709- fn last_opt( & self ) -> Option < & ' self T > { last_opt( * self ) }
1683+ fn last_opt ( & self ) -> Option < & ' self T > {
1684+ if self . len( ) == 0 { None } else { Some ( & self [ self . len( ) - 1 ] ) }
1685+ }
17101686
17111687 /**
17121688 * Find the last index matching some predicate
0 commit comments