Skip to content

Instantly share code, notes, and snippets.

@ckruse
Created December 28, 2014 20:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ckruse/d06fc4788efaa67329b1 to your computer and use it in GitHub Desktop.
Save ckruse/d06fc4788efaa67329b1 to your computer and use it in GitHub Desktop.
// -*- coding: utf-8 -*-
use std::os;
use std::sync::Future;
use std::io::BufferedReader;
use std::io::File;
fn main() {
let args = os::args();
let mut results: Vec<Future<_>> = vec![];
for file in args.slice(2, args.len()).iter() {
let path = Path::new(file);
let pattern = args[1].clone();
let delayed_value = Future::spawn(move || -> Vec<String> {
let mut retval: Vec<String> = vec![];
let fd = match File::open(&path) {
Ok(f) => f,
Err(e) => panic!("Couldn't nopen file: {}", e)
};
let mut file = BufferedReader::new(fd);
for maybe_line in file.lines() {
let line = match maybe_line {
Ok(s) => s,
Err(e) => panic!("error reading line: {}", e)
};
if line.contains(pattern.as_slice()) {
retval.push(line);
}
}
retval
});
results.push(delayed_value);
}
for i in range(0, results.len()) {
let mut result = match results.get_mut(i) {
Some(v) => v,
None => panic!("error! no value!")
};
let lines = result.get();
for line in lines.iter() {
print!("{}", line);
}
}
}
// eof
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment