Skip to content

Commit 9dfdd45

Browse files
committed
cli: grrs
1 parent 9d0075d commit 9dfdd45

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -402,3 +402,7 @@ rustup doc
402402
* `Object-Oriented`: Object-oriented programs are made up of objects. An object packages both data and the procedures that operate on that data. The procedures are typically called methods or operations.
403403
* `Polymorphism`: To many people, polymorphism is synonymous with inheritance. But it’s actually a more general concept that refers to code that can work with data of multiple types. For inheritance, those types are generally subclasses. \
404404
Rust instead uses generics to abstract over different possible types and trait bounds to impose constraints on what those types must provide. This is sometimes called bounded parametric polymorphism.
405+
406+
## Related Links
407+
408+
* [A closer look at Ownership in Rusts](https://blog.thoughtram.io/ownership-in-rust)

cli/grrs/Cargo.toml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "grrs"
3+
version = "0.1.0"
4+
authors = ["lencx <[email protected]>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
10+
structopt = '0.3.12'

cli/grrs/src/main.rs

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// use std::env;
2+
use std::fs::{self, File};
3+
use std::io::{self, BufReader, prelude::*};
4+
use std::path::Path;
5+
use structopt::StructOpt;
6+
7+
// Search for a pattern in a file and display the lines that contain it.
8+
#[derive(StructOpt)]
9+
struct Cli {
10+
/// The pattern to look for
11+
pattern: String,
12+
/// The path to the file to read
13+
#[structopt(parse(from_os_str))]
14+
path: std::path::PathBuf,
15+
}
16+
17+
fn main() {
18+
let args = Cli::from_args();
19+
if let Ok(lines) = read_line(&args.path) {
20+
for line in lines {
21+
if let Ok(_line) = line {
22+
if _line.contains(&args.pattern) {
23+
println!("{:?}", _line);
24+
}
25+
}
26+
}
27+
}
28+
}
29+
30+
/// @see: https://doc.rust-lang.org/stable/rust-by-example/std_misc/file/read_lines.html
31+
fn read_line<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
32+
where P: AsRef<Path> {
33+
let file = File::open(filename)?;
34+
Ok(io::BufReader::new(file).lines())
35+
}
36+
37+
// fn main() {
38+
// let args = Cli::from_args();
39+
// let mut content = fs::read_to_string(&args.path).expect("could not read file");
40+
41+
// for line in content.lines() {
42+
// if line.contains(&args.pattern) {
43+
// println!("{}", line);
44+
// }
45+
// }
46+
// }

0 commit comments

Comments
 (0)