Start writing a part on closures
authorRalf Jung <post@ralfj.de>
Wed, 8 Jul 2015 15:55:46 +0000 (17:55 +0200)
committerRalf Jung <post@ralfj.de>
Wed, 8 Jul 2015 15:55:46 +0000 (17:55 +0200)
src/main.rs
src/part10.rs [new file with mode: 0644]
workspace/src/part10.rs [new file with mode: 0644]

index 34751d33d69753468dfee79e5a251315e4f47242..40bb1257529dcd45c97f73305fe5f058eb3c524a 100644 (file)
@@ -89,12 +89,13 @@ mod part06;
 mod part07;
 mod part08;
 mod part09;
+mod part10;
 
 // To actually run the code of some part (after filling in the blanks, if necessary), simply edit the `main`
 // function.
 
 fn main() {
-    part00::main();
+    part10::main();
 }
 
 // Additional material
diff --git a/src/part10.rs b/src/part10.rs
new file mode 100644 (file)
index 0000000..97dd4c1
--- /dev/null
@@ -0,0 +1,60 @@
+// Rust-101, Part 09: Closures (WIP)
+// =================================
+
+use std::io::prelude::*;
+use std::io;
+
+use part05::BigInt;
+
+trait Action {
+    fn do_action(&mut self, digit: u64);
+}
+
+impl BigInt {
+    fn act_v1<A: Action>(&self, mut a: A) {
+        for digit in self {
+            a.do_action(digit);
+        }
+    }
+}
+
+struct PrintWithString {
+    prefix: String,
+}
+
+impl Action for PrintWithString {
+    fn do_action(&mut self, digit: u64) {
+        println!("{}{}", self.prefix, digit);
+    }
+}
+
+fn read_one_line() -> String {
+    println!("Please enter a line of text.");
+    let mut stdin = io::stdin();
+    let mut prefix = "".to_string();
+    stdin.read_line(&mut prefix).unwrap();
+    prefix
+}
+
+pub fn main_v1() {
+    let prefix = read_one_line();
+    let my_action = PrintWithString { prefix: prefix };
+    let bignum = BigInt::new(1 << 63) + BigInt::new(1 << 16) + BigInt::new(1 << 63);
+    bignum.act_v1(my_action);
+}
+
+impl BigInt {
+    fn act<A: FnMut(u64)>(&self, mut a: A) {
+        for digit in self {
+            a(digit);
+        }
+    }
+}
+
+pub fn main() {
+    let prefix = read_one_line();
+    let bignum = BigInt::new(1 << 63) + BigInt::new(1 << 16) + BigInt::new(1 << 63);
+    bignum.act(|digit| println!("{}{}", prefix, digit) );
+}
+
+//@ [index](main.html) | [previous](part08.html) | [next](main.html)
diff --git a/workspace/src/part10.rs b/workspace/src/part10.rs
new file mode 100644 (file)
index 0000000..ee226c4
--- /dev/null
@@ -0,0 +1,59 @@
+// Rust-101, Part 09: Closures (WIP)
+// =================================
+
+use std::io::prelude::*;
+use std::io;
+
+use part05::BigInt;
+
+trait Action {
+    fn do_action(&mut self, digit: u64);
+}
+
+impl BigInt {
+    fn act_v1<A: Action>(&self, mut a: A) {
+        for digit in self {
+            a.do_action(digit);
+        }
+    }
+}
+
+struct PrintWithString {
+    prefix: String,
+}
+
+impl Action for PrintWithString {
+    fn do_action(&mut self, digit: u64) {
+        println!("{}{}", self.prefix, digit);
+    }
+}
+
+fn read_one_line() -> String {
+    println!("Please enter a line of text.");
+    let mut stdin = io::stdin();
+    let mut prefix = "".to_string();
+    stdin.read_line(&mut prefix).unwrap();
+    prefix
+}
+
+pub fn main_v1() {
+    let prefix = read_one_line();
+    let my_action = PrintWithString { prefix: prefix };
+    let bignum = BigInt::new(1 << 63) + BigInt::new(1 << 16) + BigInt::new(1 << 63);
+    bignum.act_v1(my_action);
+}
+
+impl BigInt {
+    fn act<A: FnMut(u64)>(&self, mut a: A) {
+        for digit in self {
+            a(digit);
+        }
+    }
+}
+
+pub fn main() {
+    let prefix = read_one_line();
+    let bignum = BigInt::new(1 << 63) + BigInt::new(1 << 16) + BigInt::new(1 << 63);
+    bignum.act(|digit| println!("{}{}", prefix, digit) );
+}
+