Created
December 28, 2016 02:25
-
-
Save anonymous/a6cf1aa51bf8e26e9dda4c50b002422c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::env; | |
use std::error; | |
use std::fs::File; | |
use std::io::{self, BufRead, Write}; | |
use std::process; | |
type Error = Box<error::Error + Send + Sync>; | |
fn main() { | |
match run() { | |
Ok(count) => println!("{}", count), | |
Err(err) => { | |
let _ = writeln!(&mut io::stderr(), "{}", err); | |
process::exit(1); | |
} | |
} | |
} | |
fn run() -> Result<u64, Error> { | |
let (fpath, query) = match (env::args_os().nth(1), env::args().nth(2)) { | |
(None, _) => return Err(Error::from("missing file to search")), | |
(_, None) => return Err(Error::from("missing search query")), | |
(Some(fpath), Some(query)) => (fpath, query), | |
}; | |
let mut count = 0; | |
let mut line = String::new(); | |
let mut rdr = io::BufReader::new(File::open(fpath)?); | |
while rdr.read_line(&mut line)? > 0 { | |
if line.contains(&query) { | |
count += 1; | |
} | |
line.clear(); | |
} | |
Ok(count) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment