/src/test/run-pass/linear-for-loop.rs

http://github.com/jruderman/rust · Rust · 24 lines · 20 code · 3 blank · 1 comment · 19 complexity · 078daca5fb8b5a0b3fb2e9beed236e2f MD5 · raw file

  1. fn main() {
  2. let x = ~[1, 2, 3];
  3. let mut y = 0;
  4. for x.each |i| { log(debug, i); y += i; }
  5. log(debug, y);
  6. assert (y == 6);
  7. let s = ~"hello there";
  8. let mut i: int = 0;
  9. for str::each(s) |c| {
  10. if i == 0 { assert (c == 'h' as u8); }
  11. if i == 1 { assert (c == 'e' as u8); }
  12. if i == 2 { assert (c == 'l' as u8); }
  13. if i == 3 { assert (c == 'l' as u8); }
  14. if i == 4 { assert (c == 'o' as u8); }
  15. // ...
  16. i += 1;
  17. log(debug, i);
  18. log(debug, c);
  19. }
  20. assert (i == 11);
  21. }