More code.
Change-Id: I565975f1936bcdf993c0b17175a1906a1561130e
diff --git a/src/bin/tut1.rs b/src/bin/tut1.rs
deleted file mode 100644
index 83596ad..0000000
--- a/src/bin/tut1.rs
+++ /dev/null
@@ -1,43 +0,0 @@
-// Structs, Borrowing
-
-#![allow(dead_code)]
-
-use rust_tutorial::*;
-
-fn greet_person_1(p: Person) {
- println!("Hello {} ({})!", p.name, p.age);
-}
-
-fn greet_person_2(p: &Person) {
- println!("Hello {} ({})!", p.name, p.age);
-}
-
-fn birthday(p: &mut Person) {
- p.age += 1;
-}
-
-fn main() {
- // -- Struct instantiation --
- let mut p = Person {
- age: 30,
- name: "Mary".to_string(),
- };
-
- // -- Borrowing --
- //greet_person_1(p);
- //greet_person_1(p);
-
- greet_person_2(&p);
- greet_person_2(&p);
-
- birthday(&mut p);
-
- greet_person_2(&p);
-
- // -- Traits --
- println!("p = {}", p.to_string());
-
- // -- Debugging --
- let x = dbg!(100 + 2) + 3;
- println!("Hello, world! 100 + 2 + 3 = {}", x);
-}
diff --git a/src/bin/tut1a.rs b/src/bin/tut1a.rs
new file mode 100644
index 0000000..62b70d1
--- /dev/null
+++ b/src/bin/tut1a.rs
@@ -0,0 +1,20 @@
+// Structs, Borrowing
+
+#![allow(dead_code)]
+
+use rust_tutorial::*;
+
+fn greet_person(p: Person) {
+ println!("Hello {} ({})!", p.name, p.age);
+}
+
+fn main() {
+ // -- Struct instantiation --
+ let p = Person {
+ age: dbg!(20 + 10),
+ name: "Mary".to_string(),
+ };
+
+ //greet_person(p);
+ //greet_person(p);
+}
diff --git a/src/bin/tut1b.rs b/src/bin/tut1b.rs
new file mode 100644
index 0000000..9bd5e92
--- /dev/null
+++ b/src/bin/tut1b.rs
@@ -0,0 +1,21 @@
+// Structs, Borrowing
+
+#![allow(dead_code)]
+
+use rust_tutorial::*;
+
+fn greet_person(p: &Person) {
+ println!("Hello {} ({})!", p.name, p.age);
+}
+
+fn main() {
+ // -- Struct instantiation --
+ let p = Person {
+ age: 30,
+ name: "Mary".to_string(),
+ };
+
+ // -- Borrowing --
+ greet_person(&p);
+ greet_person(&p);
+}
diff --git a/src/bin/tut1c.rs b/src/bin/tut1c.rs
new file mode 100644
index 0000000..c8bd848
--- /dev/null
+++ b/src/bin/tut1c.rs
@@ -0,0 +1,28 @@
+// Structs, Borrowing
+
+#![allow(dead_code)]
+
+use rust_tutorial::*;
+
+fn greet_person(p: &Person) {
+ println!("Hello {} ({})!", p.name, p.age);
+}
+
+fn birthday(p: &mut Person) {
+ p.age += 1;
+}
+
+fn main() {
+ // -- Struct instantiation --
+ let mut p = Person {
+ age: 30,
+ name: "Mary".to_string(),
+ };
+
+ greet_person(&p);
+ greet_person(&p);
+
+ birthday(&mut p);
+
+ greet_person(&p);
+}
diff --git a/src/bin/tut1d.rs b/src/bin/tut1d.rs
new file mode 100644
index 0000000..4c7e54a
--- /dev/null
+++ b/src/bin/tut1d.rs
@@ -0,0 +1,31 @@
+// Structs, Borrowing
+
+#![allow(dead_code)]
+
+use rust_tutorial::*;
+
+fn greet_person(p: &Person) {
+ println!("Hello {} ({})!", p.name, p.age);
+}
+
+fn birthday(p: &mut Person) {
+ p.age += 1;
+}
+
+fn main() {
+ // -- Struct instantiation --
+ let mut p = Person {
+ age: 30,
+ name: "Mary".to_string(),
+ };
+
+ greet_person(&p);
+ greet_person(&p);
+
+ birthday(&mut p);
+
+ greet_person(&p);
+
+ // -- Traits --
+ println!("p = {}", p.to_string());
+}
diff --git a/src/bin/tut1e.rs b/src/bin/tut1e.rs
new file mode 100644
index 0000000..ec02b68
--- /dev/null
+++ b/src/bin/tut1e.rs
@@ -0,0 +1,25 @@
+// Structs, Borrowing
+
+#![allow(dead_code)]
+
+use rust_tutorial::*;
+
+pub struct Department<'a> {
+ pub boss: &'a Person
+}
+
+fn make_person(name: &str, age: i32) -> Person {
+ let p = Person { name: name.to_string(), age };
+ p
+}
+
+fn birthday(p: &mut Person) {
+ p.age += 1;
+}
+
+fn main() {
+ let mut p = make_person("Mr. P", 30);
+ let dept_1 = Department { boss: &p };
+ //birthday(&mut p);
+ println!("{}", dept_1.boss.to_string());
+}
diff --git a/src/bin/tut2.rs b/src/bin/tut2.rs
index 702b870..a308ee0 100644
--- a/src/bin/tut2.rs
+++ b/src/bin/tut2.rs
@@ -1,3 +1,5 @@
+// Fearless Abstraction
+
#![allow(dead_code)]
#![allow(unused_imports)]
@@ -6,7 +8,7 @@
/// Run this to see the disassembly of `compute_sum_of_squares_{1,2}`:
///
/// ```bash
-/// cargo objdump --bin tut2 --release -- -d | awk -v RS= '/^tut2::compute_sum_of_squares/'
+/// cargo objdump --bin tut3 --release -- -d | awk -v RS= '/^([[:xdigit:]]+ )?tut2::compute_sum_of_squares/'
/// ```
#[inline(never)]
diff --git a/src/bin/tut3.rs b/src/bin/tut3.rs
index a1005a0..84c11bf 100644
--- a/src/bin/tut3.rs
+++ b/src/bin/tut3.rs
@@ -1,4 +1,4 @@
-// Generic Programming
+// Generic Programming: Static Polymorphism
#![allow(dead_code)]
#![allow(unused_imports)]
@@ -8,7 +8,7 @@
/// Run this to see the disassembly of `compute_sum_of_squares_{1,2}`:
///
/// ```bash
-/// cargo objdump --bin tut2 --release -- -d | awk -v RS= '/^tut2::compute_sum_of_squares/'
+/// cargo objdump --bin tut3 --release -- -d | awk -v RS= '/^([[:xdigit:]]+ )?tut3::compute_sum_of_squares/'
/// ```
#[inline(never)]
diff --git a/src/bin/tut4.rs b/src/bin/tut4.rs
index f743435..2894eb6 100644
--- a/src/bin/tut4.rs
+++ b/src/bin/tut4.rs
@@ -1,9 +1,27 @@
-// Fearless Concurrency
+// Generic Programming: Dynamic Polymorphism
#![allow(dead_code)]
#![allow(unused_imports)]
use rust_tutorial::*;
+fn stringify_all(xs: Vec<&dyn ToString>) -> String {
+ let strings: Vec<String> =
+ xs.iter().map(|x| x.to_string()).collect();
+
+ strings.join(", ")
+}
+
fn main() {
+ let mut stringlike_things: Vec<&dyn ToString> = Vec::new();
+
+ let x1 = "abc";
+ let x2 = 100;
+ let x3 = Person { age: 30, name: "Mary".to_string() };
+
+ stringlike_things.push(&x1);
+ stringlike_things.push(&x2);
+ stringlike_things.push(&x3);
+
+ println!("{}", stringify_all(stringlike_things));
}
diff --git a/src/bin/tut5.rs b/src/bin/tut5.rs
new file mode 100644
index 0000000..d2e2936
--- /dev/null
+++ b/src/bin/tut5.rs
@@ -0,0 +1,31 @@
+// Fearless Concurrency
+
+#![allow(dead_code)]
+#![allow(unused_imports)]
+
+use rust_tutorial::*;
+
+use std::sync::{Mutex, Arc};
+use std::thread;
+
+fn main() {
+ let counter = Arc::new(Mutex::new(0));
+ let mut workers = vec![];
+
+ for _ in 0..10 {
+ let counter = Arc::clone(&counter);
+
+ let worker = thread::spawn(move || {
+ let mut num = counter.lock().unwrap();
+ *num += 1;
+ });
+
+ workers.push(worker);
+ }
+
+ for worker in workers {
+ worker.join().unwrap();
+ }
+
+ println!("Result: {}", *counter.lock().unwrap());
+}