1
0
mirror of https://github.com/BurntSushi/ripgrep.git synced 2025-11-23 21:54:45 +02:00
Files
ripgrep/crates/searcher/examples/search-stdin.rs
Andrew Gallant bb8172fe9b style: apply rustfmt
Maybe 2024 changes?

Note that we now set `edition = "2024"` explicitly in `rustfmt.toml`.
Without this, it seems like it's possible in some cases for rustfmt to
run under an older edition's style. Not sure how though.
2025-09-19 21:08:19 -04:00

35 lines
754 B
Rust

use std::env;
use std::error::Error;
use std::io;
use std::process;
use grep_regex::RegexMatcher;
use grep_searcher::Searcher;
use grep_searcher::sinks::UTF8;
fn main() {
if let Err(err) = example() {
eprintln!("{}", err);
process::exit(1);
}
}
fn example() -> Result<(), Box<dyn Error>> {
let pattern = match env::args().nth(1) {
Some(pattern) => pattern,
None => {
return Err(From::from(format!("Usage: search-stdin <pattern>")));
}
};
let matcher = RegexMatcher::new(&pattern)?;
Searcher::new().search_reader(
&matcher,
io::stdin(),
UTF8(|lnum, line| {
print!("{}:{}", lnum, line);
Ok(true)
}),
)?;
Ok(())
}