10
10
11
11
//! A standard, garbage-collected linked list.
12
12
13
+ use std:: container:: Container ;
14
+
13
15
#[ deriving( Clone , Eq ) ]
14
16
#[ allow( missing_doc) ]
15
17
pub enum List < T > {
@@ -53,6 +55,11 @@ impl<T> List<T> {
53
55
}
54
56
}
55
57
58
+ impl < T > Container for List < T > {
59
+ /// Returns the length of a list
60
+ fn len ( & self ) -> uint { self . iter ( ) . len ( ) }
61
+ }
62
+
56
63
/// Returns true if a list contains an element with the given value
57
64
pub fn has < T : Eq > ( list : @List < T > , element : T ) -> bool {
58
65
let mut found = false ;
@@ -70,13 +77,6 @@ pub fn is_empty<T>(list: @List<T>) -> bool {
70
77
}
71
78
}
72
79
73
- /// Returns the length of a list
74
- pub fn len < T > ( list : @List < T > ) -> uint {
75
- let mut count = 0 u;
76
- iter ( list, |_e| count += 1 u) ;
77
- count
78
- }
79
-
80
80
/// Returns all but the first element of a list
81
81
pub fn tail < T > ( list : @List < T > ) -> @List < T > {
82
82
match * list {
@@ -252,10 +252,11 @@ mod tests {
252
252
253
253
#[ test]
254
254
fn test_len ( ) {
255
- let list = @List :: from_vec ( [ 0 , 1 , 2 ] ) ;
256
- let empty = @list:: Nil :: < int > ;
257
- assert_eq ! ( list:: len( list) , 3 u) ;
258
- assert_eq ! ( list:: len( empty) , 0 u) ;
255
+ let empty = Nil :: < int > ;
256
+ assert_eq ! ( empty. len( ) , 0 u) ;
257
+
258
+ let list = List :: from_vec ( [ 0 , 1 , 2 ] ) ;
259
+ assert_eq ! ( list. len( ) , 3 u) ;
259
260
}
260
261
261
262
#[ test]
0 commit comments