diff options
| author | hybrid <hybrid@hybridlabs.cc> | 2026-10-03 04:40:21 +0300 |
|---|---|---|
| committer | hybrid <hybrid@hybridlabs.cc> | 2026-10-03 04:40:21 +0300 |
| commit | f6cbf2bf76e77c8f64312f573f46cdc7bd434acd (patch) | |
| tree | e1798d4d8533fcbfcda2b269ddf5494147466633 /src/christmas_song | |
| parent | d13f336198c40abacff6996f118040ae73dfbb30 (diff) | |
| download | rust-book-f6cbf2bf76e77c8f64312f573f46cdc7bd434acd.tar.gz rust-book-f6cbf2bf76e77c8f64312f573f46cdc7bd434acd.tar.bz2 rust-book-f6cbf2bf76e77c8f64312f573f46cdc7bd434acd.zip | |
add: 8 projects
variables, shadowing, another_function, branches, loops, fahr2cel,
fibonacci, christmax_song
Diffstat (limited to 'src/christmas_song')
| -rw-r--r-- | src/christmas_song/Cargo.toml | 6 | ||||
| -rw-r--r-- | src/christmas_song/src/main.rs | 59 |
2 files changed, 65 insertions, 0 deletions
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(), + } +} |
