From 7204f08177a638478d828be6ccccbe3c51179b3f Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Tue, 26 Sep 2023 06:43:22 -0400 Subject: [PATCH] Expressions and starting flow control in RBE --- rustbyexample/.gitignore | 6 ++--- rustbyexample/expression.rs | 25 +++++++++++++++++++ rustbyexample/flow_control.d/if_else.rs | 23 +++++++++++++++++ rustbyexample/flow_control.d/loop.d/nested.rs | 18 +++++++++++++ rustbyexample/flow_control.d/loop.d/return.rs | 13 ++++++++++ rustbyexample/flow_control.d/loop.rs | 23 +++++++++++++++++ rustbyexample/flow_control.d/while.rs | 17 +++++++++++++ 7 files changed, 122 insertions(+), 3 deletions(-) create mode 100644 rustbyexample/expression.rs create mode 100644 rustbyexample/flow_control.d/if_else.rs create mode 100644 rustbyexample/flow_control.d/loop.d/nested.rs create mode 100644 rustbyexample/flow_control.d/loop.d/return.rs create mode 100644 rustbyexample/flow_control.d/loop.rs create mode 100644 rustbyexample/flow_control.d/while.rs diff --git a/rustbyexample/.gitignore b/rustbyexample/.gitignore index d591051..307d7d8 100644 --- a/rustbyexample/.gitignore +++ b/rustbyexample/.gitignore @@ -1,6 +1,6 @@ -/*.d/* +*.d/out +*.d/**/out +expression hello primitives variable_bindings - -!/*.d/*.rs diff --git a/rustbyexample/expression.rs b/rustbyexample/expression.rs new file mode 100644 index 0000000..ac421f4 --- /dev/null +++ b/rustbyexample/expression.rs @@ -0,0 +1,25 @@ +#[allow(path_statements)] +#[allow(unused_must_use)] +fn main() { + let x = 5; + x; + x + 1; + 15; + + let x = 5u32; + + let y = { + let x_squared = x * x; + let x_cube = x_squared * x; + + x_cube + x_squared + x + }; + + let z = { + 2 * x; + }; + + println!("x is {:?}", x); + println!("y is {:?}", y); + println!("z is {:?}", z); +} diff --git a/rustbyexample/flow_control.d/if_else.rs b/rustbyexample/flow_control.d/if_else.rs new file mode 100644 index 0000000..d131213 --- /dev/null +++ b/rustbyexample/flow_control.d/if_else.rs @@ -0,0 +1,23 @@ +fn main() { + let n = 5; + + if n < 0 { + print!("{} is negative", n); + } else if n > 0 { + print!("{} is positive", n); + } else { + print!("{} is zero", n); + } + + let big_n = if n < 10 && n > -10 { + println!(", and is a small number, increase net-fold"); + + 10 * n + } else { + println!(", and is a big number, halve the number"); + + n / 2 + }; + + println!("{} -> {}", n, big_n); +} diff --git a/rustbyexample/flow_control.d/loop.d/nested.rs b/rustbyexample/flow_control.d/loop.d/nested.rs new file mode 100644 index 0000000..42dab25 --- /dev/null +++ b/rustbyexample/flow_control.d/loop.d/nested.rs @@ -0,0 +1,18 @@ +#![allow(unreachable_code)] +#![allow(unused_labels)] + +fn main() { + 'outer: loop { + println!("Entered the outer loop"); + + 'inner: loop { + println!("Entered the inner loop"); + + break 'outer; + } + + println!("This point will never be reached"); + } + + println!("Exited the outer loop"); +} diff --git a/rustbyexample/flow_control.d/loop.d/return.rs b/rustbyexample/flow_control.d/loop.d/return.rs new file mode 100644 index 0000000..41810b0 --- /dev/null +++ b/rustbyexample/flow_control.d/loop.d/return.rs @@ -0,0 +1,13 @@ +fn main() { + let mut counter = 0; + + let result = loop { + counter += 1; + + if counter == 10 { + break counter * 2; + } + }; + + assert_eq!(result, 20); +} diff --git a/rustbyexample/flow_control.d/loop.rs b/rustbyexample/flow_control.d/loop.rs new file mode 100644 index 0000000..e18a6cd --- /dev/null +++ b/rustbyexample/flow_control.d/loop.rs @@ -0,0 +1,23 @@ +fn main() { + let mut count = 0u32; + + println!("Let's count until infinity!"); + + loop { + count += 1; + + if count == 3 { + println!("three"); + + continue; + } + + println!("{}", count); + + if count == 5 { + println!("OK, that's enough"); + + break; + } + } +} diff --git a/rustbyexample/flow_control.d/while.rs b/rustbyexample/flow_control.d/while.rs new file mode 100644 index 0000000..38778dc --- /dev/null +++ b/rustbyexample/flow_control.d/while.rs @@ -0,0 +1,17 @@ +fn main() { + let mut n = 1; + + while n < 101 { + if n % 15 == 0 { + println!("fizzbuzz"); + } else if n % 3 == 0 { + println!("fizz"); + } else if n % 5 == 0 { + println!("buzz"); + } else { + println!("{}", n); + } + + n += 1; + } +}