2012-09-05 14:32:05 -05:00
|
|
|
use cmp::Eq;
|
2012-08-27 18:26:35 -05:00
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn iter<T>(v: ~[T], it: fn(T) -> bool) {
|
2012-03-27 05:33:13 -05:00
|
|
|
let mut i = 0u, l = v.len();
|
|
|
|
while i < l {
|
|
|
|
if !it(v[i]) { break; }
|
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-27 18:26:35 -05:00
|
|
|
fn find_pos<T:Eq>(n: T, h: ~[T]) -> Option<uint> {
|
2012-03-27 05:33:13 -05:00
|
|
|
let mut i = 0u;
|
2012-06-30 18:19:07 -05:00
|
|
|
for iter(h) |e| {
|
2012-08-20 14:23:37 -05:00
|
|
|
if e == n { return Some(i); }
|
2012-03-27 05:33:13 -05:00
|
|
|
i += 1u;
|
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
None
|
2012-03-27 05:33:13 -05:00
|
|
|
}
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn bail_deep(x: ~[~[bool]]) {
|
2012-03-27 05:33:13 -05:00
|
|
|
let mut seen = false;
|
2012-06-30 18:19:07 -05:00
|
|
|
for iter(x) |x| {
|
|
|
|
for iter(x) |x| {
|
2012-03-27 05:33:13 -05:00
|
|
|
assert !seen;
|
2012-08-01 19:30:05 -05:00
|
|
|
if x { seen = true; return; }
|
2012-03-27 05:33:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
assert !seen;
|
|
|
|
}
|
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
fn ret_deep() -> ~str {
|
2012-06-30 18:19:07 -05:00
|
|
|
for iter(~[1, 2]) |e| {
|
|
|
|
for iter(~[3, 4]) |x| {
|
2012-08-01 19:30:05 -05:00
|
|
|
if e + x > 4 { return ~"hi"; }
|
2012-03-27 05:33:13 -05:00
|
|
|
}
|
|
|
|
}
|
2012-08-01 19:30:05 -05:00
|
|
|
return ~"bye";
|
2012-03-27 05:33:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let mut last = 0;
|
2012-06-30 18:19:07 -05:00
|
|
|
for vec::all(~[1, 2, 3, 4, 5, 6, 7]) |e| {
|
2012-03-27 05:33:13 -05:00
|
|
|
last = e;
|
|
|
|
if e == 5 { break; }
|
2012-07-09 16:37:48 -05:00
|
|
|
if e % 2 == 1 { again; }
|
2012-03-27 05:33:13 -05:00
|
|
|
assert e % 2 == 0;
|
|
|
|
};
|
|
|
|
assert last == 5;
|
|
|
|
|
2012-08-20 14:23:37 -05: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);
|
2012-03-27 05:33:13 -05:00
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
bail_deep(~[~[false, false], ~[true, true], ~[false, true]]);
|
|
|
|
bail_deep(~[~[true]]);
|
|
|
|
bail_deep(~[~[false, false, false]]);
|
2012-03-27 05:33:13 -05:00
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
assert ret_deep() == ~"hi";
|
2012-03-27 05:33:13 -05:00
|
|
|
}
|