Created
May 20, 2015 16:16
-
-
Save anonymous/dfba59961c1e4a121f92 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This code is editable and runnable! | |
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 = 0; | |
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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment