@@ -63,13 +63,11 @@ impl<T> Container for List<T> {
6363 fn is_empty ( & self ) -> bool { match * self { Nil => true , _ => false } }
6464}
6565
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+ }
7371}
7472
7573/// Returns all but the first element of a list
@@ -208,13 +206,14 @@ mod tests {
208206 }
209207
210208 #[ 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 ) ) ) ;
218217 }
219218
220219 #[ test]
0 commit comments