diff options
author | Adam Carpenter <gitlab@53hor.net> | 2019-07-09 15:14:04 -0400 |
---|---|---|
committer | Adam Carpenter <gitlab@53hor.net> | 2019-07-09 15:14:04 -0400 |
commit | 7e8ee5ed9cad6484e9f13f81731b102ced58402e (patch) | |
tree | 5395402ab07bbb5a659dbd68c701e22a1227202f /meap/ch2/src | |
download | learning-rust-7e8ee5ed9cad6484e9f13f81731b102ced58402e.tar.xz learning-rust-7e8ee5ed9cad6484e9f13f81731b102ced58402e.zip |
Init.
Diffstat (limited to 'meap/ch2/src')
-rwxr-xr-x | meap/ch2/src/main.rs | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/meap/ch2/src/main.rs b/meap/ch2/src/main.rs new file mode 100755 index 0000000..4d16a7a --- /dev/null +++ b/meap/ch2/src/main.rs @@ -0,0 +1,121 @@ +extern crate regex; +extern crate clap; + +use regex::Regex; +use clap::{App, Arg}; +use std::fs::File; +use std::io; +use std::io::BufReader; +use std::io::prelude::*; + + +//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)) + .arg(Arg::with_name("input") + .help("file to search") + .takes_value(true)) + .get_matches(); + + let pattern = args.value_of("pattern").unwrap(); + let re = Regex::new(pattern).unwrap(); + + let input = args.value_of("input").unwrap_or("-"); + + if input == "-" { + let stdin = io::stdin(); + let reader = stdin.lock(); + process_lines(reader, re); + } + else { + let file = File::open(input).unwrap(); + let reader = BufReader::new(file); + process_lines(reader, re); + } + +} + +fn process_lines<T: BufRead + Sized>(reader: T, re: Regex) { + for line_ in reader.lines() { + let line = line_.unwrap(); + + match re.find(&line) { + Some(_) => print!("-> "), + None => print!(" "), + }; + + println!("{}", line); + } +} + +//fn add<T: Add<Output = T>>(i: T, j: T) -> T { +// i + j +//} + + +//fn add(i: i32, j: i32) -> i32 { +// i + j +//} |