rust/src/test/run-pass/ret-break-cont-in-block.rs

73 lines
1.7 KiB
Rust
Raw Normal View History

// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// xfail-fast
#[legacy_modes];
2012-09-05 12:32:05 -07:00
use cmp::Eq;
2012-08-27 16:26:35 -07:00
fn iter<T>(v: ~[T], it: fn(T) -> bool) {
let mut i = 0u, l = v.len();
while i < l {
if !it(v[i]) { break; }
i += 1u;
}
}
2012-12-05 20:14:33 -08:00
fn find_pos<T:Eq Copy>(n: T, h: ~[T]) -> Option<uint> {
let mut i = 0u;
2012-12-05 17:36:51 -08:00
for iter(copy h) |e| {
2012-08-20 12:23:37 -07:00
if e == n { return Some(i); }
i += 1u;
}
2012-08-20 12:23:37 -07:00
None
}
fn bail_deep(x: ~[~[bool]]) {
let mut seen = false;
2012-12-05 16:51:32 -08:00
for iter(copy x) |x| {
for iter(copy x) |x| {
assert !seen;
2012-08-01 17:30:05 -07:00
if x { seen = true; return; }
}
}
assert !seen;
}
fn ret_deep() -> ~str {
2012-06-30 16:19:07 -07:00
for iter(~[1, 2]) |e| {
for iter(~[3, 4]) |x| {
2012-08-01 17:30:05 -07:00
if e + x > 4 { return ~"hi"; }
}
}
2012-08-01 17:30:05 -07:00
return ~"bye";
}
fn main() {
let mut last = 0;
2012-06-30 16:19:07 -07:00
for vec::all(~[1, 2, 3, 4, 5, 6, 7]) |e| {
2012-09-27 22:20:47 -07:00
last = *e;
if *e == 5 { break; }
if *e % 2 == 1 { loop; }
assert *e % 2 == 0;
};
assert last == 5;
2012-08-20 12:23:37 -07:00
assert find_pos(1, ~[0, 1, 2, 3]) == Some(1u);
assert find_pos(1, ~[0, 4, 2, 3]) == None;
assert find_pos(~"hi", ~[~"foo", ~"bar", ~"baz", ~"hi"]) == Some(3u);
bail_deep(~[~[false, false], ~[true, true], ~[false, true]]);
bail_deep(~[~[true]]);
bail_deep(~[~[false, false, false]]);
assert ret_deep() == ~"hi";
}