diff options
| author | hybrid <hybrid@hybridlabs.cc> | 2026-10-31 03:54:43 +0300 |
|---|---|---|
| committer | hybrid <hybrid@hybridlabs.cc> | 2026-10-31 03:54:43 +0300 |
| commit | 62dcfb1d9f95503f8eb8be1b29fa1d75bfcc27e2 (patch) | |
| tree | b8461eda6cce3f9bbfb2261dd828077e17c7d226 /src/shirt-company | |
| parent | 18ad654357d6cca34782aaa269f5e8e06cbe9241 (diff) | |
| download | rust-book-62dcfb1d9f95503f8eb8be1b29fa1d75bfcc27e2.tar.gz rust-book-62dcfb1d9f95503f8eb8be1b29fa1d75bfcc27e2.tar.bz2 rust-book-62dcfb1d9f95503f8eb8be1b29fa1d75bfcc27e2.zip | |
add: 7 craits
smartpointers
interior_mutability
threads
mutex
hello-async
async_concurrency
multi_futures
Diffstat (limited to '')
| -rw-r--r-- | src/shirt-company/Cargo.toml | 6 | ||||
| -rw-r--r-- | src/shirt-company/src/main.rs | 52 |
2 files changed, 58 insertions, 0 deletions
diff --git a/src/shirt-company/Cargo.toml b/src/shirt-company/Cargo.toml new file mode 100644 index 0000000..4f22291 --- /dev/null +++ b/src/shirt-company/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "shirt-company" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/src/shirt-company/src/main.rs b/src/shirt-company/src/main.rs new file mode 100644 index 0000000..2c87d69 --- /dev/null +++ b/src/shirt-company/src/main.rs @@ -0,0 +1,52 @@ +#[derive(Debug, PartialEq, Copy, Clone)] +enum ShirtColor { + Red, + Blue, +} + +struct Inventory { + shirts: Vec<ShirtColor>, +} + +impl Inventory { + fn giveaway(&self, user_preference: Option<ShirtColor>) -> ShirtColor { + user_preference.unwrap_or_else(|| self.most_stocked()) + } + + fn most_stocked(&self) -> ShirtColor { + let mut num_red = 0; + let mut num_blue = 0; + + for color in &self.shirts { + match color { + ShirtColor::Red => num_red += 1, + ShirtColor::Blue => num_blue += 1, + } + } + if num_red > num_blue { + ShirtColor::Red + } else { + ShirtColor::Blue + } + } +} + +fn main() { + let store = Inventory { + shirts: vec![ShirtColor::Blue, ShirtColor::Red, ShirtColor::Blue], + }; + + let user_pref1 = Some(ShirtColor::Red); + let giveaway1 = store.giveaway(user_pref1); + println!( + "The user with preference {:?} gets {:?}", + user_pref1, giveaway1 + ); + + let user_pref2 = None; + let giveaway2 = store.giveaway(user_pref2); + println!( + "The user with preference {:?} gets {:?}", + user_pref2, giveaway2 + ); +} |
