From 93b1c04a218858ecc59b6b8929103695b7b8c2a0 Mon Sep 17 00:00:00 2001 From: Carlos Maiolino Date: Sat, 6 Sep 2025 09:30:14 +0200 Subject: Move rust playground here Signed-off-by: Carlos Maiolino --- rust/chap3/flow.rs | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 rust/chap3/flow.rs (limited to 'rust/chap3') diff --git a/rust/chap3/flow.rs b/rust/chap3/flow.rs new file mode 100644 index 0000000..36557e5 --- /dev/null +++ b/rust/chap3/flow.rs @@ -0,0 +1,50 @@ + +fn if_me() { + let number = 6; + + if number < 5 { + println!("{number}"); + } else { + println!("Number too high"); + } + + let choice = false; + + let b = if choice { 24 } else { 64 }; + + println!("Choice is {b}"); +} + +fn loop_me() { + + let mut c = 5; + + /* + * Rust allow loops to me labeled, and use such labels into + * break and continue commands to decide what loop to break/continue + */ + let res = 'loop_label: loop { + println!("Again"); + + if c == 0 { + break 'loop_label 99; // Return a value from the loop to the variable + } + + c = c - 1; + }; + + println!("Value returned by the loop {res}"); +} + +fn for_me() { + unimplemented!(); +} + +fn while_me() { + unimplemented!(); +} + +fn main() { + if_me(); + loop_me(); +} -- cgit v1.2.3