From 5ceb6d9743e4857849dd3b6b14bbea39a667c0c0 Mon Sep 17 00:00:00 2001
From: darkicewolf50 <brock.tomlinson@ucalgary.ca>
Date: Sun, 29 Dec 2024 11:57:22 -0700
Subject: [PATCH] got through a good portion of loops

---
 .gitignore                            |  6 +-
 functions_rust/src/main.rs            |  2 +-
 guessing_game/target/.rustc_info.json |  2 +-
 hello_cargo/target/.rustc_info.json   |  2 +-
 loops/Cargo.lock                      |  7 +++
 loops/Cargo.toml                      |  6 ++
 loops/src/main.rs                     | 80 +++++++++++++++++++++++++++
 7 files changed, 99 insertions(+), 6 deletions(-)
 create mode 100644 loops/Cargo.lock
 create mode 100644 loops/Cargo.toml
 create mode 100644 loops/src/main.rs

diff --git a/.gitignore b/.gitignore
index 58bcaa2..109f371 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,5 +1,5 @@
 /target
-
 /**/target/
-
-/*/target/
\ No newline at end of file
+/*/target/
+/**/target
+/*/target
\ No newline at end of file
diff --git a/functions_rust/src/main.rs b/functions_rust/src/main.rs
index 5b621e6..bf1329f 100644
--- a/functions_rust/src/main.rs
+++ b/functions_rust/src/main.rs
@@ -37,11 +37,11 @@ fn example_function (x:i8, y:u16) {
 //      let x = 5;
 //      x + 1
 //     }
+// macros are statements
 
 //  EXPRESSIONS DO NOT END WITH ;
 // if you add a ; to the end of a statment it will turn into a statement and not return anything
 // unless it is a return keyword
-// or if it is a marco (not required)
 
 
 fn with_return_plus_one (c: i16) -> i16 {
diff --git a/guessing_game/target/.rustc_info.json b/guessing_game/target/.rustc_info.json
index 1b8bd1d..5e569d1 100644
--- a/guessing_game/target/.rustc_info.json
+++ b/guessing_game/target/.rustc_info.json
@@ -1 +1 @@
-{"rustc_fingerprint":13662911779824945272,"outputs":{"12744816824612481171":{"success":true,"status":"","code":0,"stdout":"___.exe\nlib___.rlib\n___.dll\n___.dll\n___.lib\n___.dll\nC:\\Users\\Brock\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\npacked\n___\ndebug_assertions\npanic=\"unwind\"\nproc_macro\ntarget_abi=\"\"\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"msvc\"\ntarget_family=\"windows\"\ntarget_feature=\"cmpxchg16b\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_feature=\"sse3\"\ntarget_has_atomic=\"128\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_os=\"windows\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"pc\"\nwindows\n","stderr":""},"4614504638168534921":{"success":true,"status":"","code":0,"stdout":"rustc 1.83.0 (90b35a623 2024-11-26)\nbinary: rustc\ncommit-hash: 90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\ncommit-date: 2024-11-26\nhost: x86_64-pc-windows-msvc\nrelease: 1.83.0\nLLVM version: 19.1.1\n","stderr":""},"15729799797837862367":{"success":true,"status":"","code":0,"stdout":"___.exe\nlib___.rlib\n___.dll\n___.dll\n___.lib\n___.dll\nC:\\Users\\Brock\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\npacked\n___\ndebug_assertions\npanic=\"unwind\"\nproc_macro\ntarget_abi=\"\"\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"msvc\"\ntarget_family=\"windows\"\ntarget_feature=\"cmpxchg16b\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_feature=\"sse3\"\ntarget_has_atomic=\"128\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_os=\"windows\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"pc\"\nwindows\n","stderr":""}},"successes":{}}
\ No newline at end of file
+{"rustc_fingerprint":13662911779824945272,"outputs":{"4614504638168534921":{"success":true,"status":"","code":0,"stdout":"rustc 1.83.0 (90b35a623 2024-11-26)\nbinary: rustc\ncommit-hash: 90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\ncommit-date: 2024-11-26\nhost: x86_64-pc-windows-msvc\nrelease: 1.83.0\nLLVM version: 19.1.1\n","stderr":""},"15729799797837862367":{"success":true,"status":"","code":0,"stdout":"___.exe\nlib___.rlib\n___.dll\n___.dll\n___.lib\n___.dll\nC:\\Users\\Brock\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\npacked\n___\ndebug_assertions\npanic=\"unwind\"\nproc_macro\ntarget_abi=\"\"\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"msvc\"\ntarget_family=\"windows\"\ntarget_feature=\"cmpxchg16b\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_feature=\"sse3\"\ntarget_has_atomic=\"128\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_os=\"windows\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"pc\"\nwindows\n","stderr":""}},"successes":{}}
\ No newline at end of file
diff --git a/hello_cargo/target/.rustc_info.json b/hello_cargo/target/.rustc_info.json
index 35ae81c..5e569d1 100644
--- a/hello_cargo/target/.rustc_info.json
+++ b/hello_cargo/target/.rustc_info.json
@@ -1 +1 @@
-{"rustc_fingerprint":13662911779824945272,"outputs":{"15729799797837862367":{"success":true,"status":"","code":0,"stdout":"___.exe\nlib___.rlib\n___.dll\n___.dll\n___.lib\n___.dll\nC:\\Users\\Brock\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\npacked\n___\ndebug_assertions\npanic=\"unwind\"\nproc_macro\ntarget_abi=\"\"\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"msvc\"\ntarget_family=\"windows\"\ntarget_feature=\"cmpxchg16b\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_feature=\"sse3\"\ntarget_has_atomic=\"128\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_os=\"windows\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"pc\"\nwindows\n","stderr":""},"4614504638168534921":{"success":true,"status":"","code":0,"stdout":"rustc 1.83.0 (90b35a623 2024-11-26)\nbinary: rustc\ncommit-hash: 90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\ncommit-date: 2024-11-26\nhost: x86_64-pc-windows-msvc\nrelease: 1.83.0\nLLVM version: 19.1.1\n","stderr":""},"12744816824612481171":{"success":true,"status":"","code":0,"stdout":"___.exe\nlib___.rlib\n___.dll\n___.dll\n___.lib\n___.dll\nC:\\Users\\Brock\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\npacked\n___\ndebug_assertions\npanic=\"unwind\"\nproc_macro\ntarget_abi=\"\"\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"msvc\"\ntarget_family=\"windows\"\ntarget_feature=\"cmpxchg16b\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_feature=\"sse3\"\ntarget_has_atomic=\"128\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_os=\"windows\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"pc\"\nwindows\n","stderr":""}},"successes":{}}
\ No newline at end of file
+{"rustc_fingerprint":13662911779824945272,"outputs":{"4614504638168534921":{"success":true,"status":"","code":0,"stdout":"rustc 1.83.0 (90b35a623 2024-11-26)\nbinary: rustc\ncommit-hash: 90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\ncommit-date: 2024-11-26\nhost: x86_64-pc-windows-msvc\nrelease: 1.83.0\nLLVM version: 19.1.1\n","stderr":""},"15729799797837862367":{"success":true,"status":"","code":0,"stdout":"___.exe\nlib___.rlib\n___.dll\n___.dll\n___.lib\n___.dll\nC:\\Users\\Brock\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\npacked\n___\ndebug_assertions\npanic=\"unwind\"\nproc_macro\ntarget_abi=\"\"\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"msvc\"\ntarget_family=\"windows\"\ntarget_feature=\"cmpxchg16b\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_feature=\"sse3\"\ntarget_has_atomic=\"128\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_os=\"windows\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"pc\"\nwindows\n","stderr":""}},"successes":{}}
\ No newline at end of file
diff --git a/loops/Cargo.lock b/loops/Cargo.lock
new file mode 100644
index 0000000..7261438
--- /dev/null
+++ b/loops/Cargo.lock
@@ -0,0 +1,7 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 4
+
+[[package]]
+name = "loops"
+version = "0.1.0"
diff --git a/loops/Cargo.toml b/loops/Cargo.toml
new file mode 100644
index 0000000..810e8bb
--- /dev/null
+++ b/loops/Cargo.toml
@@ -0,0 +1,6 @@
+[package]
+name = "loops"
+version = "0.1.0"
+edition = "2021"
+
+[dependencies]
diff --git a/loops/src/main.rs b/loops/src/main.rs
new file mode 100644
index 0000000..1b41ae1
--- /dev/null
+++ b/loops/src/main.rs
@@ -0,0 +1,80 @@
+fn main() {
+    println!("Hello, world!");
+    looping_through_a_list();
+}
+
+// rust has 3 types of loops: loop, while and for
+
+// This will not stop until interrupted unless told by a return, break or a ctrl-c
+fn forever_loop () {
+    loop {
+        println!("another loop!")
+    }
+
+    let mut counter = 0;
+    let value_from_loop = loop {
+        counter += 1;
+        if counter == 4 {
+            break counter;
+        }
+    };
+}
+
+// continue
+// this will start the loop over again from the top regardless of the code after it
+// break, can return values from a loop, needs a ; after break to do that
+    
+// one use of a loop is checking weather a thread is finished or do a operation that you know might fail
+
+fn loop_with_labels () {
+    let mut counter = 0;
+    let result = 'first_loop: loop {
+        println!("counter = {counter}");
+        let mut remaining = 10;
+        'inner_loop: loop {
+            println!("remaining = {remaining}");
+            if remaining == 9 {
+                // both acceptable
+                break 'inner_loop;
+                // continue 'inner_loop;
+            }
+            if counter == 2 {
+                // returns a value and goes out of the outermost loop called/labelled first_loop
+                break 'first_loop counter*2;
+            }
+            remaining -= 1;
+        }
+        counter += 1;
+    };
+    println!("counter = {counter}");
+    println!("result = {result}");
+}
+
+fn looping_with_while () {
+    let mut count_down = 3;
+    println!("liftoff in {count_down} seconds");
+
+    while count_down != 0 {
+        println!("{}!", count_down);
+
+        count_down -= 1;
+    }
+
+    println!("Lift-Off!");
+
+}
+
+// while will only run when the condition/bool is true otherwise it will call a break of the loop by itself
+// removes a lot of the nesting of if, else, and break statements in loops
+
+fn looping_through_a_list () {
+    let a = [32, 5, 20, 40, 50, 60];
+    let mut index = 0;
+    // if a.len was a static number this would/could cause the program to panic 
+    // because if a was modified and the static number was not then it would attempt to go out out the assigned memory
+    // using list.len() is always a better solution
+    while index < a.len() {
+        println!("the value is: {}", a[index]);
+        index += 1;
+    }
+}
\ No newline at end of file