rust/src/test/run-pass/block-arg.rs

53 lines
1.5 KiB
Rust
Raw Normal View History

2011-12-29 09:49:33 -08:00
// Check usage and precedence of block arguments in expressions:
fn main() {
let v = [-1f, 0f, 1f, 2f, 3f];
// Statement form does not require parentheses:
vec::iter(v) { |i|
log(info, i);
}
// Usable at all:
2012-01-05 14:46:14 +01:00
let any_negative = vec::any(v) { |e| float::is_negative(e) };
2011-12-29 09:49:33 -08:00
assert any_negative;
// Higher precedence than assignments:
2012-01-05 14:46:14 +01:00
any_negative = vec::any(v) { |e| float::is_negative(e) };
2011-12-29 09:49:33 -08:00
assert any_negative;
// Higher precedence than unary operations:
let abs_v = vec::map(v) { |e| float::abs(e) };
2012-01-05 14:46:14 +01:00
assert vec::all(abs_v) { |e| float::is_nonnegative(e) };
assert !vec::any(abs_v) { |e| float::is_negative(e) };
2011-12-29 09:49:33 -08:00
// Usable in funny statement-like forms:
2012-01-05 14:46:14 +01:00
if !vec::any(v) { |e| float::is_positive(e) } {
2011-12-29 09:49:33 -08:00
assert false;
}
2012-01-05 14:46:14 +01:00
alt vec::all(v) { |e| float::is_negative(e) } {
2011-12-29 09:49:33 -08:00
true { fail "incorrect answer."; }
false { }
}
alt 3 {
2012-01-05 14:46:14 +01:00
_ when vec::any(v) { |e| float::is_negative(e) } {
2011-12-29 09:49:33 -08:00
}
_ {
fail "wrong answer.";
}
}
// Lower precedence than binary operations:
let w = vec::foldl(0f, v, { |x, y| x + y }) + 10f;
let y = vec::foldl(0f, v) { |x, y| x + y } + 10f;
let z = 10f + vec::foldl(0f, v) { |x, y| x + y };
assert w == y;
assert y == z;
// They are not allowed as the tail of a block without parentheses:
let w =
2012-01-05 14:46:14 +01:00
if true { vec::any(abs_v, { |e| float::is_nonnegative(e) }) }
2011-12-29 09:49:33 -08:00
else { false };
assert w;
}