@@ -53,26 +53,6 @@ impl<T> List<T> {
53
53
}
54
54
}
55
55
56
- /**
57
- * Search for an element that matches a given predicate
58
- *
59
- * Apply function `f` to each element of `list`, starting from the first.
60
- * When function `f` returns true then an option containing the element
61
- * is returned. If `f` matches no elements then none is returned.
62
- */
63
- pub fn find < T : Clone > ( list : @List < T > , f: |& T | -> bool) -> Option < T > {
64
- let mut list = list;
65
- loop {
66
- list = match * list {
67
- Cons ( ref head, tail) => {
68
- if f ( head) { return Some ( ( * head) . clone ( ) ) ; }
69
- tail
70
- }
71
- Nil => return None
72
- }
73
- } ;
74
- }
75
-
76
56
/**
77
57
* Returns true if a list contains an element that matches a given predicate
78
58
*
@@ -196,8 +176,6 @@ mod tests {
196
176
use list:: { List , Nil , head, is_empty, tail} ;
197
177
use list;
198
178
199
- use std:: option;
200
-
201
179
#[ test]
202
180
fn test_iter ( ) {
203
181
let list = List :: from_vec ( [ 0 , 1 , 2 ] ) ;
@@ -254,18 +232,21 @@ mod tests {
254
232
255
233
#[ test]
256
234
fn test_find_success ( ) {
257
- fn match_ ( i : & int ) -> bool { return * i == 2 ; }
258
- let list = @List :: from_vec ( [ 0 , 1 , 2 ] ) ;
259
- assert_eq ! ( list:: find( list, match_) , option:: Some ( 2 ) ) ;
235
+ fn match_ ( i : & & int ) -> bool { * * i == 2 }
236
+
237
+ let list = List :: from_vec ( [ 0 , 1 , 2 ] ) ;
238
+ assert_eq ! ( list. iter( ) . find( match_) . unwrap( ) , & 2 ) ;
260
239
}
261
240
262
241
#[ test]
263
242
fn test_find_fail ( ) {
264
- fn match_ ( _i : & int ) -> bool { return false ; }
265
- let list = @List :: from_vec ( [ 0 , 1 , 2 ] ) ;
266
- let empty = @list:: Nil :: < int > ;
267
- assert_eq ! ( list:: find( list, match_) , option:: None :: <int>) ;
268
- assert_eq ! ( list:: find( empty, match_) , option:: None :: <int>) ;
243
+ fn match_ ( _i : & & int ) -> bool { false }
244
+
245
+ let empty = Nil :: < int > ;
246
+ assert_eq ! ( empty. iter( ) . find( match_) , None ) ;
247
+
248
+ let list = List :: from_vec ( [ 0 , 1 , 2 ] ) ;
249
+ assert_eq ! ( list. iter( ) . find( match_) , None ) ;
269
250
}
270
251
271
252
#[ test]
0 commit comments