Skip to content

Commit

Permalink
Rustハンズオン@エウレカ社 (#34)
Browse files Browse the repository at this point in the history
* feat: handson > cat

* feat: handson > grep
  • Loading branch information
zaki-yama authored Jul 19, 2021
1 parent 995760c commit 34f8b6d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
18 changes: 18 additions & 0 deletions rust-handson/rsgrep/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
### https://raw.github.com/github/gitignore/3bb7b4b767f3f8df07e362dfa03c8bd425f16d32/Rust.gitignore

# Generated by Cargo
# will have compiled files and executables
debug/
target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb


11 changes: 11 additions & 0 deletions rust-handson/rsgrep/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "rsgrep"
version = "0.1.0"
authors = ["Shingo Yamazaki <[email protected]>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rayon = "1.5.1"
structopt = "0.3.22"
34 changes: 34 additions & 0 deletions rust-handson/rsgrep/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use rayon::prelude::*;
use std::{env::args, fs::read_to_string};
use structopt::StructOpt;

#[derive(StructOpt)]
#[structopt(name = "rsgrep")]
struct GrepArgs {
#[structopt(name = "PATTERN")]
pattern: String,
#[structopt(name = "FILE")]
path: Vec<String>,
}

fn grep(state: &GrepArgs, content: String, file_name: &str) {
for line in content.lines() {
if line.contains(state.pattern.as_str()) {
println!("{}: {}", file_name, line);
}
}
}

fn run(state: GrepArgs) {
state
.path
.par_iter()
.for_each(|file| match read_to_string(file) {
Ok(content) => grep(&state, content, file),
Err(reason) => println!("{}", reason),
});
}

fn main() {
run(GrepArgs::from_args());
}

0 comments on commit 34f8b6d

Please sign in to comment.