Skip to content

Commit d681907

Browse files
committed
Removed list::find() in favor of iter().find()
1 parent a09a4b8 commit d681907

File tree

1 file changed

+11
-30
lines changed

1 file changed

+11
-30
lines changed

src/libcollections/list.rs

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -53,26 +53,6 @@ impl<T> List<T> {
5353
}
5454
}
5555

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-
7656
/**
7757
* Returns true if a list contains an element that matches a given predicate
7858
*
@@ -196,8 +176,6 @@ mod tests {
196176
use list::{List, Nil, head, is_empty, tail};
197177
use list;
198178

199-
use std::option;
200-
201179
#[test]
202180
fn test_iter() {
203181
let list = List::from_vec([0, 1, 2]);
@@ -254,18 +232,21 @@ mod tests {
254232

255233
#[test]
256234
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);
260239
}
261240

262241
#[test]
263242
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);
269250
}
270251

271252
#[test]

0 commit comments

Comments
 (0)