From: Ralf Jung Date: Wed, 8 Jul 2015 15:55:46 +0000 (+0200) Subject: Start writing a part on closures X-Git-Url: https://git.ralfj.de/rust-101.git/commitdiff_plain/e73ddf5e1d4768cb86fba3eb583f4fec0286acff?hp=-c Start writing a part on closures --- e73ddf5e1d4768cb86fba3eb583f4fec0286acff 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) ); +} +