diff --git a/control_flow/Cargo.lock b/control_flow/Cargo.lock new file mode 100644 index 0000000..84fb6b8 --- /dev/null +++ b/control_flow/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "control_flow" +version = "0.1.0" diff --git a/control_flow/Cargo.toml b/control_flow/Cargo.toml new file mode 100644 index 0000000..8eb83b9 --- /dev/null +++ b/control_flow/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "control_flow" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/control_flow/src/main.rs b/control_flow/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/control_flow/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/functions/Cargo.lock b/functions/Cargo.lock new file mode 100644 index 0000000..82f83a1 --- /dev/null +++ b/functions/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "functions" +version = "0.1.0" diff --git a/functions/Cargo.toml b/functions/Cargo.toml new file mode 100644 index 0000000..a9b5578 --- /dev/null +++ b/functions/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "functions" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/functions/src/main.rs b/functions/src/main.rs new file mode 100644 index 0000000..e63cdf0 --- /dev/null +++ b/functions/src/main.rs @@ -0,0 +1,22 @@ +fn main() { + println!("Hi there, "); + + a_called_function(); // we just call this function without any parameters + + another_function(5, 6); // we call this function with two parameters + + print_label_measurement(5, 'h'); +} + +fn a_called_function() { + println!("I'm a_called_function!"); +} + +// in function signatures, we MUST define the type of each parameter value +fn another_function(x: i32, y: i32) { + println!("The value of x: {} and the value of y: {}", x, y); +} + +fn print_label_measurement(value: i32, unit_label: char) { + println!("The measurement is: {value} {unit_label}"); +} diff --git a/functions/{directory} b/functions/{directory} new file mode 100644 index 0000000..e69de29 diff --git a/guessing_game/Cargo.lock b/guessing_game/Cargo.lock new file mode 100644 index 0000000..0fb52b3 --- /dev/null +++ b/guessing_game/Cargo.lock @@ -0,0 +1,75 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "getrandom" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "190092ea657667030ac6a35e305e62fc4dd69fd98ac98631e5d3a2b1575a12b5" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "guessing_game" +version = "0.1.0" +dependencies = [ + "rand", +] + +[[package]] +name = "libc" +version = "0.2.153" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" + +[[package]] +name = "ppv-lite86" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" + +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" diff --git a/guessing_game/Cargo.toml b/guessing_game/Cargo.toml new file mode 100644 index 0000000..7eda67a --- /dev/null +++ b/guessing_game/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "guessing_game" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +rand = "0.8.5" diff --git a/guessing_game/README.md b/guessing_game/README.md new file mode 100644 index 0000000..85e9cd4 --- /dev/null +++ b/guessing_game/README.md @@ -0,0 +1,4 @@ +# Taken from the Rust Book + +Basic concepts of the language, used to make a very simple number guessing game. +Should be commented enough to be understandable. diff --git a/guessing_game/src/main.rs b/guessing_game/src/main.rs new file mode 100644 index 0000000..419af64 --- /dev/null +++ b/guessing_game/src/main.rs @@ -0,0 +1,34 @@ +use std::io; +use std::cmp::Ordering; +use rand::Rng; + +fn main() { + println!("Guess the number!"); + + let secret_number: u32 = rand::thread_rng().gen_range(1..=100); + + loop { + println!("Please input your guess."); + + let mut guess = String::new(); + + io::stdin() + .read_line(&mut guess) + .expect("Failed to read line"); + + let guess: u32 = match guess.trim().parse() { + Ok(num) => num, + Err(_) => continue, + }; + + println!("You guessed: {guess}"); + + match guess.cmp(&secret_number) { + Ordering::Less => println!("Too small!"), + Ordering::Greater => println!("Too big!"), + Ordering::Equal => {println!("You win!"); + break; + } + } + } +} diff --git a/variables/Cargo.lock b/variables/Cargo.lock new file mode 100644 index 0000000..a6daf1d --- /dev/null +++ b/variables/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "variables" +version = "0.1.0" diff --git a/variables/Cargo.toml b/variables/Cargo.toml new file mode 100644 index 0000000..f1cad76 --- /dev/null +++ b/variables/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "variables" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/variables/src/main.rs b/variables/src/main.rs new file mode 100644 index 0000000..4726bfc --- /dev/null +++ b/variables/src/main.rs @@ -0,0 +1,100 @@ +fn main() { + let x: i32 = 5; + + let x = x + 1; + + { + let x = x * 2; + println!("The value of x in the inner scope is: {x}"); + } + + println!("The value of x in the outer scope is: {x}"); + + let x = 2.0; // will be f64 the arch of CPU - as no explicit type given + + let y: f32 = 3.0; // will be f32 as explicit type given + + println!("x: {x}, y: {y}"); + + // addition + let sum = 5 + 10; + + // subtraction + let difference = 95.5 - 4.3; + + // multiplication + let product = 4 * 30; + + // division + let quotient = 56.7 / 32.2; + let truncated = -5 / 3; // will give -1 + + // remainder, properly called modulo + let remainder = 43 % 5; + + // boolean + let tr_ue = true; // with implicit type + let fal_se: bool = false; // with explicit type + + // char types are 4 bytes in size and represent a Unicode Scalar Value + let c = 'z'; + let z: char = 'ℤ'; // with explicit type + let heart_eyed_cat = '😻'; + + // printing the values so compiler doesn't complain about unused variables + println!("x: {x}, sum: {sum}, difference: {difference}, product: {product}, quotient: {quotient}, truncated: {truncated}, remainder: {remainder}, tr_ue: {tr_ue}, fal_se: {fal_se}, c: {c}, z: {z}, heart_eyed_cat: {heart_eyed_cat}"); + + // tuple + let tup: (i32, f64, u8) = (500, 6.4, 1); + + let (x, y, z) = tup; // destructuring the tuple + + println!("x: {x}, y: {y}, z: {z}"); + + let x: (i32, f64, u8) = (500, 6.4, 1); // tuples can hold different data types + + let five_hundred = x.0; // accessing the first element of the tuple and + let six_point_four = x.1; // the rest, by tuple index number, using a period + let one = x.2; // followed by the index number on the variable + + print!("five_hundred: {five_hundred}, six_point_four: {six_point_four}, one: {one}"); + + // array + let a = [1, 2, 3, 4, 5]; // arrays must contain the same data types + + println!("a0: {}, a1: {}, a2: {}, a3: {}, a4: {}", + a[0], a[1], a[2], a[3], a[4]); + + // arrays are useful when you want your data allocated on the stack, not + // the heap, and you know the size of the array at compile time + let months: [&str; 12] = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; + + println!("month 1: {}, month 2: {}, month 3: {}, month 4: {}, month 5: {}, month 6: {}, month 7: {}, month 8: {}, month 9: {}, month 10: {}, month 11: {}, month 12: {}", months[0], months[1], months[2], months[3], months[4], months[5], months[6], months[7], months[8], months[9], months[10], months[11]); + + // here we are specifying the type and length of the array [i32; 5] + // and then giving it our actual values to store in the array + let a: [i32; 5] = [1, 2, 3, 4, 5]; + + let b = [3; 5]; // this will create an array that contains 5 elements + // each with the value of 3 + println!("b0: {}, b1: {}, b2: {}, b3: {}, b4: {}", + b[0], b[1], b[2], b[3], b[4]); + + // just like tuples, we can destructure arrays + let first = a[0]; // accessing the first element of the array [1] + let second = a[1]; // accessing the second element of the array [2] + + // both the tuple and the array structures are zero-indexed and (start at 0) + // and the length of the tuple or array is fixed, meaning it cannot grow or + // shrink. If you try and access an index that is out of range, your program + // will panic at runtime! + + println!("first: {first}, second: {second}"); + + let t = ([1; 2], [3; 4]); + + let (a, _b) = t; + + println!("{}", a[0] + t.1[0]); + +}