From 67cdcc2e12118becb823e20a40cc2687f2b8425a Mon Sep 17 00:00:00 2001 From: Adam Carpenter <53hornet@gmail.com> Date: Wed, 27 Mar 2019 15:32:37 -0400 Subject: Started Rust in Action MEAP. --- meap/ch2/src/main.rs | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 meap/ch2/src/main.rs (limited to 'meap/ch2/src') diff --git a/meap/ch2/src/main.rs b/meap/ch2/src/main.rs new file mode 100644 index 0000000..055ec1c --- /dev/null +++ b/meap/ch2/src/main.rs @@ -0,0 +1,100 @@ +extern crate regex; +extern crate clap; + +use regex::Regex; +use clap::{App, Arg}; +//use std::ops::{Add}; + +fn main() { +// let a = 10; +// let b: i32 = 20; +// +// let c = add(a, b); +// println!("a + b = {}", c); + + +// let twenty = 20; +// let twenty_one: i32 = twenty + 1; +// let floats_okay = 21.0; +// let million = 1_000_000; +// +// println!("{} {} {} {}", twenty, twenty_one, floats_okay, million); +// +// let three = 0b11; +// let thirty = 0o36; +// let three_hundred = 0x12c; +// +// println!("{} {} {}", three, thirty, three_hundred); +// println!("{:b} {:b} {:b}", three, thirty, three_hundred); +// println!("{:o} {:o} {:o}", three, thirty, three_hundred); +// println!("{:x} {:x} {:x}", three, thirty, three_hundred); + + +// //let needle = 42; +// let haystack = [1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862]; +// +// for reference in haystack.iter() { +// let item = *reference; +// +// //if item == needle { +// // println!("{}", item); +// //} +// +// //if reference == &needle { +// // println!("{}", reference); +// //} +// +// let result = match item { +// 42 | 132 => "hit", +// _ => "miss", +// }; +// +// println!("{}", result); +// } + + + // let (a, b) = (1.2, 3.4); + // let (x, y) = (10, 20); + + // let c = add(a, b); + // let z = add(x, y); + + // println!("{} {}", c, z); + + + let args = App::new("ch2") + .version("0.1") + .about("searches for patterns") + .arg(Arg::with_name("pattern") + .help("the pattern to search for") + .takes_value(true) + .required(true)) + .get_matches(); + + let pattern = args.value_of("pattern").unwrap(); + let re = Regex::new(pattern).unwrap(); + let quote = "Every face, every shop, bedroom window, public-house, \ + and\ndark square is a picture feverishly turned--in search \ + of what?\nIt is the same with books. What do we seek through \ + millions of pages?"; + + for (index, line) in quote.lines().enumerate() { + match re.find(line) { + Some(_) => print!("-> "), + None => print!(" "), + }; + + let line_num = index + 1; + println!("{}: {}", line_num, line); + } + +} + +//fn add>(i: T, j: T) -> T { +// i + j +//} + + +//fn add(i: i32, j: i32) -> i32 { +// i + j +//} -- cgit v1.2.3