diff options
author | Jesse Luehrs <doy@tozt.net> | 2014-07-02 02:54:21 -0400 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2014-07-02 02:54:21 -0400 |
commit | e620daf3b1a40ae825c625bc4328fb7d2b3ee8b6 (patch) | |
tree | faa469408ee78623eaa8d2720dcbb36096788ca1 /examples/02_sample.rs | |
download | intro-to-rust-yapc-na-2014-e620daf3b1a40ae825c625bc4328fb7d2b3ee8b6.tar.gz intro-to-rust-yapc-na-2014-e620daf3b1a40ae825c625bc4328fb7d2b3ee8b6.zip |
Diffstat (limited to 'examples/02_sample.rs')
-rw-r--r-- | examples/02_sample.rs | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/examples/02_sample.rs b/examples/02_sample.rs new file mode 100644 index 0000000..4df0563 --- /dev/null +++ b/examples/02_sample.rs @@ -0,0 +1,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() { + match token { + '+' => accumulator += 1, + '-' => accumulator -= 1, + '*' => accumulator *= 2, + '/' => accumulator /= 2, + _ => { /* ignore everything else */ } + } + } + + println!("The program \"{}\" calculates the value {}", + program, accumulator); +} + |