From e73ddf5e1d4768cb86fba3eb583f4fec0286acff Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Wed, 8 Jul 2015 17:55:46 +0200 Subject: [PATCH] Start writing a part on closures --- src/main.rs | 3 ++- src/part10.rs | 60 +++++++++++++++++++++++++++++++++++++++++ workspace/src/part10.rs | 59 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 src/part10.rs create mode 100644 workspace/src/part10.rs diff --git a/src/main.rs b/src/main.rs index 34751d3..40bb125 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 index 0000000..97dd4c1 --- /dev/null +++ b/src/part10.rs @@ -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(&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(&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 index 0000000..ee226c4 --- /dev/null +++ b/workspace/src/part10.rs @@ -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(&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(&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) ); +} + -- 2.30.2