summaryrefslogtreecommitdiff
path: root/rust/theBook/chapter-8-common-collections/hash
diff options
context:
space:
mode:
Diffstat (limited to 'rust/theBook/chapter-8-common-collections/hash')
-rw-r--r--rust/theBook/chapter-8-common-collections/hash/Cargo.toml8
-rw-r--r--rust/theBook/chapter-8-common-collections/hash/src/main.rs30
2 files changed, 38 insertions, 0 deletions
diff --git a/rust/theBook/chapter-8-common-collections/hash/Cargo.toml b/rust/theBook/chapter-8-common-collections/hash/Cargo.toml
new file mode 100644
index 0000000..7e2371d
--- /dev/null
+++ b/rust/theBook/chapter-8-common-collections/hash/Cargo.toml
@@ -0,0 +1,8 @@
+[package]
+name = "hash"
+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-8-common-collections/hash/src/main.rs b/rust/theBook/chapter-8-common-collections/hash/src/main.rs
new file mode 100644
index 0000000..d1e9a4c
--- /dev/null
+++ b/rust/theBook/chapter-8-common-collections/hash/src/main.rs
@@ -0,0 +1,30 @@
+use std::collections::HashMap as hm;
+
+fn main() {
+ let mut scores = hm::new();
+
+ scores.insert(String::from("yellow"),10);
+ scores.insert(String::from("blue"),50);
+
+ let tm = String::from("blue");
+
+ let score = scores.get(&tm).copied().unwrap_or(0);
+
+ println!("{score}");
+
+ count_word();
+}
+
+
+fn count_word() {
+ let text = "hello world wonderful world";
+
+ let mut hp = hm::new();
+
+ for word in text.split_whitespace() {
+ let count = hp.entry(word).or_insert(0); // or_insert return reference
+ *count += 1;
+ }
+
+ println!("{:#?}", hp);
+}