diff options
| -rw-r--r-- | Cargo.lock | 32 | ||||
| -rw-r--r-- | Cargo.toml | 8 | ||||
| -rw-r--r-- | src/another_function/Cargo.toml | 6 | ||||
| -rw-r--r-- | src/another_function/src/main.rs | 8 | ||||
| -rw-r--r-- | src/branches/Cargo.toml | 6 | ||||
| -rw-r--r-- | src/branches/src/main.rs | 9 | ||||
| -rw-r--r-- | src/christmas_song/Cargo.toml | 6 | ||||
| -rw-r--r-- | src/christmas_song/src/main.rs | 59 | ||||
| -rw-r--r-- | src/fahr2cel/Cargo.toml | 6 | ||||
| -rw-r--r-- | src/fahr2cel/src/main.rs | 31 | ||||
| -rw-r--r-- | src/fibonacci/Cargo.toml | 6 | ||||
| -rw-r--r-- | src/fibonacci/src/main.rs | 36 | ||||
| -rw-r--r-- | src/loops/Cargo.toml | 6 | ||||
| -rw-r--r-- | src/loops/src/main.rs | 13 | ||||
| -rw-r--r-- | src/shadowing/Cargo.toml | 6 | ||||
| -rw-r--r-- | src/shadowing/src/main.rs | 12 | ||||
| -rw-r--r-- | src/variables/Cargo.toml | 6 | ||||
| -rw-r--r-- | src/variables/src/main.rs | 7 |
18 files changed, 263 insertions, 0 deletions
@@ -3,6 +3,14 @@ version = 4 [[package]] +name = "another_function" +version = "0.1.0" + +[[package]] +name = "branches" +version = "0.1.0" + +[[package]] name = "cfg-if" version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -20,6 +28,10 @@ dependencies = [ ] [[package]] +name = "christmas_song" +version = "0.1.0" + +[[package]] name = "cpufeatures" version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -29,6 +41,14 @@ dependencies = [ ] [[package]] +name = "fahr2cel" +version = "0.1.0" + +[[package]] +name = "fibonacci" +version = "0.1.0" + +[[package]] name = "getrandom" version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -62,6 +82,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3eaf3ede3fee6db1a4c2ee091bf8a8b4dccdc6d17f656fb07896ee72867612f2" [[package]] +name = "loops" +version = "0.1.0" + +[[package]] name = "r-efi" version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -83,3 +107,11 @@ name = "rand_core" version = "0.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "63b8176103e19a2643978565ca18b50549f6101881c443590420e4dc998a3c69" + +[[package]] +name = "shadowing" +version = "0.1.0" + +[[package]] +name = "variables" +version = "0.1.0" @@ -3,5 +3,13 @@ members = [ "src/hello_world", "src/hello_cargo", "src/guessing_game", + "src/variables", + "src/shadowing", + "src/another_function", + "src/branches", + "src/loops", + "src/fahr2cel", + "src/fibonacci", + "src/christmas_song", ] resolver = "2" diff --git a/src/another_function/Cargo.toml b/src/another_function/Cargo.toml new file mode 100644 index 0000000..7d60c8a --- /dev/null +++ b/src/another_function/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "another_function" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/src/another_function/src/main.rs b/src/another_function/src/main.rs new file mode 100644 index 0000000..9da0175 --- /dev/null +++ b/src/another_function/src/main.rs @@ -0,0 +1,8 @@ +fn main() { + println!("First argument is: {}", print_labeled_measurement(5, 'h')); +} + +fn print_labeled_measurement(value: i32, unit_label: char) -> i32 { + println!("The measurement is: {value}{unit_label}"); + value +} diff --git a/src/branches/Cargo.toml b/src/branches/Cargo.toml new file mode 100644 index 0000000..b12fd97 --- /dev/null +++ b/src/branches/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "branches" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/src/branches/src/main.rs b/src/branches/src/main.rs new file mode 100644 index 0000000..7fdaaaf --- /dev/null +++ b/src/branches/src/main.rs @@ -0,0 +1,9 @@ +fn main() { + let number = 6; + + if number < 5 { + println!("condition was true"); + } else { + println!("condition was false"); + } +} diff --git a/src/christmas_song/Cargo.toml b/src/christmas_song/Cargo.toml new file mode 100644 index 0000000..25474b6 --- /dev/null +++ b/src/christmas_song/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "christmas_song" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/src/christmas_song/src/main.rs b/src/christmas_song/src/main.rs new file mode 100644 index 0000000..b04524d --- /dev/null +++ b/src/christmas_song/src/main.rs @@ -0,0 +1,59 @@ +fn main() { + const DAYS_AND_GIFTS: [[&str; 3]; 12] = [ + ["first", "a", "brand new SCV"], + ["second", "two", "Terran Wraiths"], + ["third", "three", "Marines"], + ["fourth", "four", "Hydralisks"], + ["fifth", "five", "newborn Queens"], + ["sixth", "six", "Zealots fighting"], + ["seventh", "seven", "Zerglings swarming"], + ["eighth", "eight", "Archons burning"], + ["ninth", "nine", "Battlecruisers"], + ["tenth", "ten", "Ultralisks"], + ["eleventh", "eleven", "Science Vessels"], + ["twelfth", "twelve", "Arbiters"], + ]; + + for i in 0..12 { + println!("On the {} day of Christmas, Blizzard gave to me:", + DAYS_AND_GIFTS[i][0]); + + let mut gifts = String::new(); + + for j in 0..=i { + gifts = match j { + 0 => format!("{} {}.\n", + DAYS_AND_GIFTS[j][1], + DAYS_AND_GIFTS[j][2],), + 1 => format!("{} {} and {gifts}", + DAYS_AND_GIFTS[j][1], + DAYS_AND_GIFTS[j][2]), + _ => format!("{}, {gifts}", + match i { + 8 if j == 7 => "power overwhelming".to_string(), + 9 if j == 7 => "terror all-consuming".to_string(), + 10 if j == 7 => "I hate all this singing".to_string(), + _ => format!("{} {}", + DAYS_AND_GIFTS[j][1], + DAYS_AND_GIFTS[j][2]), + } + ) + } + } + + println!("{}", capitalize(&gifts)); + } + + println!("Why am I so jolly?\n"); + println!("Hey! Who spiked the egg nog?"); +} + +fn capitalize(s: &str) -> String { + let mut chars = s.chars(); + + match chars.next() { + Some(first) => first.to_uppercase().collect::<String>() + + chars.as_str(), + None => String::new(), + } +} diff --git a/src/fahr2cel/Cargo.toml b/src/fahr2cel/Cargo.toml new file mode 100644 index 0000000..e407553 --- /dev/null +++ b/src/fahr2cel/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "fahr2cel" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/src/fahr2cel/src/main.rs b/src/fahr2cel/src/main.rs new file mode 100644 index 0000000..4ea4776 --- /dev/null +++ b/src/fahr2cel/src/main.rs @@ -0,0 +1,31 @@ +use std::io; + +fn main() { + println!("Enter \"q\" to quit."); + + loop { + let mut user_input = String::new(); + + println!("Enter temperature in °F:"); + + io::stdin() + .read_line(&mut user_input) + .expect("Failed to read line."); + + if user_input.trim() == "q" { + println!("Exiting."); + break; + } + + let user_input: f64 = match user_input.trim().parse() { + Ok(num) => num, + Err(_) => continue, + }; + + println!("{user_input}°F -> °C: {:.2}", fahr2cel(user_input)); + } +} + +fn fahr2cel (fahr: f64) -> f64 { + (fahr - 32.0) * 5.0 / 9.0 +} diff --git a/src/fibonacci/Cargo.toml b/src/fibonacci/Cargo.toml new file mode 100644 index 0000000..15ac334 --- /dev/null +++ b/src/fibonacci/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "fibonacci" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/src/fibonacci/src/main.rs b/src/fibonacci/src/main.rs new file mode 100644 index 0000000..e5e973a --- /dev/null +++ b/src/fibonacci/src/main.rs @@ -0,0 +1,36 @@ +use std::io; + +fn main() { + println!("Enter n:"); + + loop { + let mut user_input = String::new(); + + io::stdin() + .read_line(&mut user_input) + .expect("Failed to read line."); + + if user_input.trim() == "q" { + println!("Exiting."); + break; + } + + let n: i32 = match user_input.trim().parse() { + Ok(num) => num, + Err(_) => continue, + }; + + let mut a: u64 = 0; + let mut b: u64 = 1; + + // calc fibonacci[n] + for i in 0..n { + let next: u64 = a.checked_add(b) + .unwrap_or_else(|| panic!("overflow at {}", i)); + a = b; + b = next; + } + + println!("fibonacci[{n}]: {a}"); + } +} diff --git a/src/loops/Cargo.toml b/src/loops/Cargo.toml new file mode 100644 index 0000000..a046a76 --- /dev/null +++ b/src/loops/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "loops" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/src/loops/src/main.rs b/src/loops/src/main.rs new file mode 100644 index 0000000..683d18b --- /dev/null +++ b/src/loops/src/main.rs @@ -0,0 +1,13 @@ +fn main() { + let mut counter = 0; + + let result = loop { + counter += 1; + + if counter == 10 { + break counter * 2; + } + }; + + println!("The result is {result}"); +} diff --git a/src/shadowing/Cargo.toml b/src/shadowing/Cargo.toml new file mode 100644 index 0000000..23403b4 --- /dev/null +++ b/src/shadowing/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "shadowing" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/src/shadowing/src/main.rs b/src/shadowing/src/main.rs new file mode 100644 index 0000000..03924fe --- /dev/null +++ b/src/shadowing/src/main.rs @@ -0,0 +1,12 @@ +fn main() { + let x = 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 is: {x}"); +} diff --git a/src/variables/Cargo.toml b/src/variables/Cargo.toml new file mode 100644 index 0000000..c093eb6 --- /dev/null +++ b/src/variables/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "variables" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/src/variables/src/main.rs b/src/variables/src/main.rs new file mode 100644 index 0000000..2500651 --- /dev/null +++ b/src/variables/src/main.rs @@ -0,0 +1,7 @@ +fn main() { + let mut x: i32 = 5; + println!("The value of x is: {}", x); + + x = 6; + println!("The value of x is: {}", x); +} |
