blob: 3fc0bffc16f64b0b829e711a89f8e339275ea535 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
fn main() {
// A simple integer calculator:
// `+` or `-` means add or subtract by 1
// `*` or `/` means multiply or divide by 2
let program = "+ + * - /";
let mut accumulator = 0i;
for token in program.chars().filter(|&x| { x != ' ' }) {
match token {
'+' => accumulator += 1,
'-' => accumulator -= 1,
'*' => accumulator *= 2,
'/' => accumulator /= 2,
_ => fail!("unknown character"),
}
}
println!("The program \"{}\" calculates the value {}",
program, accumulator);
}
|