@@ -63,13 +63,11 @@ impl<T> Container for List<T> {
63
63
fn is_empty ( & self ) -> bool { match * self { Nil => true , _ => false } }
64
64
}
65
65
66
- /// Returns true if a list contains an element with the given value
67
- pub fn has < T : Eq > ( list : @List < T > , element : T ) -> bool {
68
- let mut found = false ;
69
- each ( list, |e| {
70
- if * e == element { found = true ; false } else { true }
71
- } ) ;
72
- return found;
66
+ impl < T : Eq > List < T > {
67
+ /// Returns true if a list contains an element with the given value
68
+ pub fn contains ( & self , element : T ) -> bool {
69
+ self . iter ( ) . any ( |list_element| * list_element == element)
70
+ }
73
71
}
74
72
75
73
/// Returns all but the first element of a list
@@ -208,13 +206,14 @@ mod tests {
208
206
}
209
207
210
208
#[ test]
211
- fn test_has ( ) {
212
- let list = @List :: from_vec ( [ 5 , 8 , 6 ] ) ;
213
- let empty = @list:: Nil :: < int > ;
214
- assert ! ( ( list:: has( list, 5 ) ) ) ;
215
- assert ! ( ( !list:: has( list, 7 ) ) ) ;
216
- assert ! ( ( list:: has( list, 8 ) ) ) ;
217
- assert ! ( ( !list:: has( empty, 5 ) ) ) ;
209
+ fn test_contains ( ) {
210
+ let empty = Nil :: < int > ;
211
+ assert ! ( ( !empty. contains( 5 ) ) ) ;
212
+
213
+ let list = List :: from_vec ( [ 5 , 8 , 6 ] ) ;
214
+ assert ! ( ( list. contains( 5 ) ) ) ;
215
+ assert ! ( ( !list. contains( 7 ) ) ) ;
216
+ assert ! ( ( list. contains( 8 ) ) ) ;
218
217
}
219
218
220
219
#[ test]
0 commit comments