@@ -61,6 +61,14 @@ impl<T> List<T> {
61
61
Cons ( ref head, _) => Some ( head)
62
62
}
63
63
}
64
+
65
+ /// Returns all but the first element of a list
66
+ pub fn tail ( & self ) -> Option < @List < T > > {
67
+ match * self {
68
+ Nil => None ,
69
+ Cons ( _, tail) => Some ( tail)
70
+ }
71
+ }
64
72
}
65
73
66
74
impl < T > Container for List < T > {
@@ -78,14 +86,6 @@ impl<T:Eq> List<T> {
78
86
}
79
87
}
80
88
81
- /// Returns all but the first element of a list
82
- pub fn tail < T > ( list : @List < T > ) -> @List < T > {
83
- match * list {
84
- Cons ( _, tail) => return tail,
85
- Nil => fail ! ( "list empty" )
86
- }
87
- }
88
-
89
89
/// Appends one list to another
90
90
pub fn append < T : Clone + ' static > ( list : @List < T > , other : @List < T > ) -> @List < T > {
91
91
match * list {
@@ -117,7 +117,7 @@ fn push<T:Clone>(ll: &mut @list<T>, vv: T) {
117
117
118
118
#[ cfg( test) ]
119
119
mod tests {
120
- use list:: { List , Nil , tail } ;
120
+ use list:: { List , Nil } ;
121
121
use list;
122
122
123
123
#[ test]
@@ -143,13 +143,13 @@ mod tests {
143
143
144
144
#[ test]
145
145
fn test_from_vec ( ) {
146
- let list = @ List :: from_vec ( [ 0 , 1 , 2 ] ) ;
146
+ let list = List :: from_vec ( [ 0 , 1 , 2 ] ) ;
147
147
assert_eq ! ( list. head( ) . unwrap( ) , & 0 ) ;
148
148
149
- let mut tail = tail ( list ) ;
149
+ let mut tail = list . tail ( ) . unwrap ( ) ;
150
150
assert_eq ! ( tail. head( ) . unwrap( ) , & 1 ) ;
151
151
152
- tail = tail ( tail) ;
152
+ tail = tail. tail ( ) . unwrap ( ) ;
153
153
assert_eq ! ( tail. head( ) . unwrap( ) , & 2 ) ;
154
154
}
155
155
0 commit comments