summaryrefslogtreecommitdiff
path: root/rust/theBook/chapter-13-functional-language-features-iterators-and-closures
diff options
context:
space:
mode:
Diffstat (limited to 'rust/theBook/chapter-13-functional-language-features-iterators-and-closures')
-rw-r--r--rust/theBook/chapter-13-functional-language-features-iterators-and-closures/shirt_inventory/Cargo.lock7
-rw-r--r--rust/theBook/chapter-13-functional-language-features-iterators-and-closures/shirt_inventory/Cargo.toml8
-rw-r--r--rust/theBook/chapter-13-functional-language-features-iterators-and-closures/shirt_inventory/src/main.rs46
3 files changed, 61 insertions, 0 deletions
diff --git a/rust/theBook/chapter-13-functional-language-features-iterators-and-closures/shirt_inventory/Cargo.lock b/rust/theBook/chapter-13-functional-language-features-iterators-and-closures/shirt_inventory/Cargo.lock
new file mode 100644
index 0000000..13a1f7e
--- /dev/null
+++ b/rust/theBook/chapter-13-functional-language-features-iterators-and-closures/shirt_inventory/Cargo.lock
@@ -0,0 +1,7 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 3
+
+[[package]]
+name = "shirt_inventory"
+version = "0.1.0"
diff --git a/rust/theBook/chapter-13-functional-language-features-iterators-and-closures/shirt_inventory/Cargo.toml b/rust/theBook/chapter-13-functional-language-features-iterators-and-closures/shirt_inventory/Cargo.toml
new file mode 100644
index 0000000..6c9fe6b
--- /dev/null
+++ b/rust/theBook/chapter-13-functional-language-features-iterators-and-closures/shirt_inventory/Cargo.toml
@@ -0,0 +1,8 @@
+[package]
+name = "shirt_inventory"
+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/rust/theBook/chapter-13-functional-language-features-iterators-and-closures/shirt_inventory/src/main.rs b/rust/theBook/chapter-13-functional-language-features-iterators-and-closures/shirt_inventory/src/main.rs
new file mode 100644
index 0000000..51e1785
--- /dev/null
+++ b/rust/theBook/chapter-13-functional-language-features-iterators-and-closures/shirt_inventory/src/main.rs
@@ -0,0 +1,46 @@
+#[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 red_num = 0;
+ let mut blue_num = 0;
+
+ for color in &self.shirts {
+ match color {
+ ShirtColor::Red => red_num += 1,
+ ShirtColor::Blue => blue_num += 1,
+ }
+ }
+ if red_num > blue_num {
+ 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 {:?} get {:?}",user_pref1,giveaway1);
+
+ let user_pref2 = None;
+ let giveaway2 = store.giveaway(user_pref2);
+ println!("The user with preference {user_pref2:?} get {:?}", giveaway2);
+}