From 4571175568bcce1544d7c6da5e38841cb2377735 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Mon, 12 Mar 2012 18:26:31 -0700 Subject: stdlib: Make list::find do what the docs say it does. Talked on #rust about this change, got approval from graydon and brson. Will bring up tomorrow at meeting to verify. --- src/libstd/list.rs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/list.rs b/src/libstd/list.rs index 9f4254f20c4..a33ba0b647a 100644 --- a/src/libstd/list.rs +++ b/src/libstd/list.rs @@ -40,13 +40,13 @@ Apply function `f` to each element of `v`, starting from the first. When function `f` returns true then an option containing the element is returned. If `f` matches no elements then none is returned. "] -fn find(ls: list, f: fn(T) -> option) - -> option { +fn find(ls: list, f: fn(T) -> bool) -> option { let ls = ls; loop { alt ls { cons(hd, tl) { - alt f(hd) { none { ls = *tl; } some(rs) { ret some(rs); } } + if f(hd) { ret some(hd); } + ls = *tl; } nil { ret none; } } @@ -195,16 +195,14 @@ mod tests { #[test] fn test_find_success() { - fn match(&&i: int) -> option { - ret if i == 2 { option::some(i) } else { option::none:: }; - } + fn match(&&i: int) -> bool { ret i == 2; } let l = from_vec([0, 1, 2]); assert (list::find(l, match) == option::some(2)); } #[test] fn test_find_fail() { - fn match(&&_i: int) -> option { ret option::none::; } + fn match(&&_i: int) -> bool { ret false; } let l = from_vec([0, 1, 2]); let empty = list::nil::; assert (list::find(l, match) == option::none::); -- cgit 1.4.1-3-g733a5