summaryrefslogblamecommitdiff
path: root/meap/ch2/src/main.rs
blob: 055ec1c6c600e806197ab7e2ade821294fa339ee (plain) (tree)



































































































                                                                               
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<T: Add<Output = T>>(i: T, j: T) -> T {
//    i + j
//}


//fn add(i: i32, j: i32) -> i32 {
//    i + j
//}