summaryrefslogtreecommitdiff
path: root/_includes/example.rs
diff options
context:
space:
mode:
Diffstat (limited to '_includes/example.rs')
-rw-r--r--_includes/example.rs22
1 files changed, 22 insertions, 0 deletions
diff --git a/_includes/example.rs b/_includes/example.rs
new file mode 100644
index 0000000..7691b23
--- /dev/null
+++ b/_includes/example.rs
@@ -0,0 +1,22 @@
+// 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);
+}