diff options
| author | Carlos Maiolino <[email protected]> | 2025-07-10 22:24:20 +0200 |
|---|---|---|
| committer | Carlos Maiolino <[email protected]> | 2025-07-10 22:24:20 +0200 |
| commit | 869e68986aa8f69af6e7842260a68d1e5c6f796f (patch) | |
| tree | 63b6b5ffc3d19414233d4629a533c0d9bf3cbf72 /rust/loops/src/main.rs | |
| parent | 20834dcc57537cd95260a4a22f5d91a027adfd35 (diff) | |
Add a bunch of code
Signed-off-by: Carlos Maiolino <[email protected]>
Diffstat (limited to 'rust/loops/src/main.rs')
| -rw-r--r-- | rust/loops/src/main.rs | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/rust/loops/src/main.rs b/rust/loops/src/main.rs new file mode 100644 index 0000000..2458311 --- /dev/null +++ b/rust/loops/src/main.rs @@ -0,0 +1,62 @@ +fn main() { + let mut counter = 0; + + let result = loop { + counter += 1; + + if counter == 10 { + // loop {} is an expression and can return values, this is sick. + break counter * 2; + } + }; + + println!("The result is {result}"); + + let mut count = 0; + 'counting_up: loop { // Loop label 'counting_up + println!("count = {count}"); + let mut remaining = 10; + + loop { + println!("remaining = {remaining}"); + if remaining == 9 { + break; + } + + if count == 2 { + break 'counting_up; // Break the outter loop + } + remaining -= 1; + } + + count += 1; + } + println!("End count = {count}"); + + let mut number = 3; + + while number != 0 { + println!("Number: {number}"); + number -= 1; + } + println!("While is gone"); + + let a = [10, 20, 30, 40, 50]; + let mut index = 0; + + while index < 5 { + println!("{}", a[index]); + index += 1; + } + + // Using a for loop + for element in a { + println!("the value is: {element}"); + } + + // Range - .rev == reverse + for number in (1..4).rev() { + println!("{number}"); + } + +} |
