Skip to content

Instantly share code, notes, and snippets.

@swuecho
Forked from anonymous/playground.rs
Last active August 29, 2015 14:21
Show Gist options
  • Save swuecho/c1a7d23743d9708103c5 to your computer and use it in GitHub Desktop.
Save swuecho/c1a7d23743d9708103c5 to your computer and use it in GitHub Desktop.
// 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