summaryrefslogtreecommitdiff
path: root/collections
diff options
context:
space:
mode:
Diffstat (limited to 'collections')
-rw-r--r--collections/.gitignore18
-rw-r--r--collections/Cargo.toml9
-rw-r--r--collections/src/main.rs167
3 files changed, 194 insertions, 0 deletions
diff --git a/collections/.gitignore b/collections/.gitignore
new file mode 100644
index 0000000..e629269
--- /dev/null
+++ b/collections/.gitignore
@@ -0,0 +1,18 @@
+
+# Created by https://www.toptal.com/developers/gitignore/api/rust
+# Edit at https://www.toptal.com/developers/gitignore?templates=rust
+
+### Rust ###
+# Generated by Cargo
+# will have compiled files and executables
+/target/
+
+# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
+# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
+Cargo.lock
+
+# These are backup files generated by rustfmt
+**/*.rs.bk
+
+# End of https://www.toptal.com/developers/gitignore/api/rust
+
diff --git a/collections/Cargo.toml b/collections/Cargo.toml
new file mode 100644
index 0000000..1aa2ecc
--- /dev/null
+++ b/collections/Cargo.toml
@@ -0,0 +1,9 @@
+[package]
+name = "collections"
+version = "0.1.0"
+authors = ["Jordan Gong <jordan.gong@protonmail.com>"]
+edition = "2018"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
diff --git a/collections/src/main.rs b/collections/src/main.rs
new file mode 100644
index 0000000..0930ce9
--- /dev/null
+++ b/collections/src/main.rs
@@ -0,0 +1,167 @@
+fn main() {
+ // Vector
+ let v: Vec<i32> = Vec::new();
+
+ // Macro
+ let v = vec![1, 2, 3];
+
+ // Update
+ let mut v = Vec::new();
+
+ v.push(5);
+ v.push(6);
+ v.push(7);
+ v.push(8);
+
+ // Free when out of scope
+ {
+ let v = vec![1, 2, 3];
+ }
+
+ // Access
+ let mut v = vec![1, 2, 3, 4, 5];
+
+ let third: &i32 = &v[2];
+ println!("The third element is {}", third);
+
+ match v.get(2) {
+ Some(third) => println!("The third element is {}", third),
+ None => println!("There is no third element."),
+ }
+
+ // immutable borrow
+ // let first = &v[0];
+ let first = v[0];
+
+ v.push(6);
+
+ println!("The first element is: {}", first);
+
+ // Iterating
+ let v = vec![100, 32, 57];
+ for i in &v {
+ println!("{}", i);
+ }
+
+ // Iterating(mutable)
+ let mut v = vec![100, 32, 57];
+ for i in &mut v {
+ *i += 50;
+ }
+
+ // Storing multiple types using enum
+ #[derive(Debug)]
+ enum SpreadSheetCell {
+ Int(i32),
+ Float(f64),
+ Text(String),
+ }
+
+ let row = vec![
+ SpreadSheetCell::Int(3),
+ SpreadSheetCell::Text(String::from("blue")),
+ SpreadSheetCell::Float(10.12),
+ ];
+
+ for (i, cell) in row.iter().enumerate() {
+ println!("Cell {} is {:?}", i, cell);
+ }
+
+ // Push string
+ let mut s = String::from("foo");
+ s.push_str("bar");
+
+ let mut s = String::from("ol");
+ s.push('l');
+
+ // Concatenation
+ let s1 = String::from("tic");
+ let s2 = String::from("tac");
+ let s3 = String::from("toe");
+
+ // take owership of s1, append s2 and s3 to it
+ let s = s1 + "-" + &s2 + "-" + &s3;
+ println!("{}", s);
+
+ let s1 = String::from("tic");
+ let s2 = String::from("tac");
+ let s3 = String::from("toe");
+
+ let s = format!("{}-{}-{}", s1, s2, s3);
+ println!("{}", s);
+
+ // Iterating
+ for char in "你好世界".chars() {
+ println!("{}", char);
+ }
+
+ for byte in "你好世界".bytes() {
+ println!("{}", byte);
+ }
+
+ // Hash map
+ use std::collections::HashMap;
+
+ let mut scores = HashMap::new();
+
+ scores.insert(String::from("Blue"), 10);
+ scores.insert(String::from("Yellow"), 50);
+
+ // Using `collect` method
+ let teams = vec![String::from("Blue"), String::from("Yellow")];
+ let initial_scores = vec![10, 50];
+
+ let mut scores: HashMap<_, _> = teams.into_iter().zip(initial_scores.into_iter()).collect();
+
+ // Owership
+ let field_name = String::from("Favorite color");
+ let field_value = String::from("Blue");
+
+ let mut map = HashMap::new();
+ map.insert(field_name, field_value);
+
+ // field_name and field_value is invaid now
+ // println!("field_name: {}, field_value: {}", field_name, field_value);
+
+ // Access
+ let team_name = String::from("Blue");
+
+ let score = scores.get(&team_name);
+ match score {
+ Some(score) => println!("{}: {}", team_name, score),
+ None => println!("{} not found", team_name),
+ }
+
+ // if let Some(score) = score {
+ // println!("{}: {}", team_name, score);
+ // } else {
+ // println!("{} not found", team_name);
+ // }
+
+ for (key, value) in &scores {
+ println!("{}: {}", key, value);
+ }
+
+ // Updating(overwriting)
+ scores.insert(String::from("Blue"), 25);
+
+ println!("{:?}", scores);
+
+ // Updating(insert if not exist)
+ scores.entry(String::from("Red")).or_insert(50);
+ scores.entry(String::from("Blue")).or_insert(50);
+
+ println!("{:?}", scores);
+
+ // Updating(based on old value)
+ let text = "hello world wonderful world";
+
+ let mut map = HashMap::new();
+
+ for word in text.split_whitespace() {
+ let count = map.entry(word).or_insert(0);
+ *count += 1;
+ }
+
+ println!("{:?}", map);
+}