diff --git a/src/libcore/dlist.rs b/src/libcore/dlist.rs index c66f094eec06..06548e632b97 100644 --- a/src/libcore/dlist.rs +++ b/src/libcore/dlist.rs @@ -441,6 +441,16 @@ mod tests { assert iter::foldl(l, 0, |accum,elem| accum+elem) == 5050; } #[test] + fn test_dlist_break_early() { + let l = from_vec(~[1,2,3,4,5]); + let mut x = 0; + for l.each |i| { + x += 1; + if (i == 3) { break; } + } + assert x == 3; + } + #[test] fn test_dlist_remove_head() { let l = create::(); l.assert_consistent(); let one = l.push_n(1); diff --git a/src/libcore/iter-trait/dlist.rs b/src/libcore/iter-trait/dlist.rs index d34ea38034de..b1a822a5bad1 100644 --- a/src/libcore/iter-trait/dlist.rs +++ b/src/libcore/iter-trait/dlist.rs @@ -18,7 +18,7 @@ fn EACH(self: IMPL_T, f: fn(A) -> bool) { !box::ptr_eq(*option::get(nobe.root), *self) { fail "Iteration encountered a dlist node not on this dlist." } - f(nobe.data); + if !f(nobe.data) { break; } // Check that the user didn't do a remove. // Note that this makes it ok for the user to remove the node and then // immediately put it back in a different position. I allow this.