rust/tests/run-pass/loops.rs

36 lines
555 B
Rust
Raw Normal View History

2016-03-13 09:31:29 -05:00
fn factorial_loop() -> i64 {
let mut product = 1;
let mut i = 1;
2016-03-12 21:32:24 -06:00
2016-03-13 09:31:29 -05:00
while i <= 10 {
product *= i;
i += 1;
}
2016-03-12 21:32:24 -06:00
2016-03-13 09:31:29 -05:00
product
}
2016-03-21 04:34:24 -05:00
fn index_for_loop() -> usize {
let mut sum = 0;
let a = [0, 10, 20, 30];
for i in 0..a.len() {
sum += a[i];
}
sum
}
fn for_loop() -> usize {
let mut sum = 0;
let a = [0, 10, 20, 30];
for &n in &a {
sum += n;
}
sum
}
2016-04-22 03:34:14 -05:00
2016-04-22 07:38:46 -05:00
fn main() {
assert_eq!(factorial_loop(), 3628800);
assert_eq!(index_for_loop(), 60);
assert_eq!(for_loop(), 60);
}