Updated 'src/io_project/accepting_cli_arguments.md'.

This commit is contained in:
Hector PENG
2026-03-27 14:51:10 +08:00
parent 635bebfb8b
commit 8beb5e988c
9 changed files with 58 additions and 189 deletions

View File

@@ -1,14 +1,6 @@
[package]
name = "minigrep"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
edition = "2024"
[dependencies]
[profile.dev]
opt-level = 1
[profile.release]
opt-level = 3

View File

@@ -1,3 +0,0 @@
在文件 poem.txt 中检索to
Are you nobody, too?
How dreary to be somebody!

View File

@@ -1,9 +0,0 @@
I'm nobody! Who are you?
Are you nobody, too?
Then there's a pair of us - don't tell!
They'd banish us, you know.
How dreary to be somebody!
How public, like a frog
To tell your name the livelong day
To an admiring bog!

View File

@@ -1,33 +0,0 @@
use std::env;
pub struct Config {
pub query: String,
pub file_path: String,
pub ignore_case: bool,
}
impl Config {
pub fn build(
mut args: impl Iterator<Item = String>,
) -> Result<Config, &'static str> {
args.next();
let query = match args.next() {
Some(arg) => arg,
None => return Err("未曾获取到查询字串"),
};
let file_path = match args.next() {
Some(arg) => arg,
None => return Err("未曾获取到文件路径"),
};
let ignore_case = env::var("IGNORE_CASE").is_ok();
Ok(Config {
query,
file_path,
ignore_case,
})
}
}

View File

@@ -1,57 +0,0 @@
#![allow(warnings)]
//
// this is to disable warnings.
// Comment it to enable warnings.
//
use std::error::Error;
use std::fs;
use data_structures::Config;
//
// 以下两种写法,也是可以的
//
// use self::data_structures::Config;
// use crate::data_structures::Config;
#[cfg(test)]
mod tests;
pub mod data_structures;
pub fn run(
config: Config
) -> Result<(), Box<dyn Error>> {
let contents = fs::read_to_string(config.file_path)?;
let results: Vec<&str> = if config.ignore_case {
search_insensitive(&config.query, &contents)
} else {
search(&config.query, &contents)
};
for line in results {
println! ("{line}");
}
Ok(())
}
pub fn search<'a>(
query: &str,
contents: &'a str
) -> Vec<&'a str> {
contents
.lines()
.filter(|line| line.contains(query))
.collect()
}
pub fn search_insensitive<'a>(
query: &str,
contents: &'a str
) -> Vec<&'a str> {
let query = query.to_lowercase();
contents
.lines()
.filter(|line| line.to_lowercase().contains(&query))
.collect()
}

View File

@@ -1,19 +1,13 @@
use std::env;
use std::process;
use minigrep::data_structures::Config;
fn main() {
let config = Config::build(env::args())
.unwrap_or_else(|err| {
eprintln! ("解析参数时遇到问题:{err}");
process::exit(1);
});
let args: Vec<String> = env::args().collect();
println! ("在文件 {} 中检索:{}", config.file_path, config.query);
let query = &args[1];
let file_path = &args[2];
if let Err(e) = minigrep::run(config) {
eprintln! ("应用程序错误:{e}");
process::exit(1);
}
println! ("
在文件 {file_path}
检索 {query}
");
}

View File

@@ -1,28 +0,0 @@
use super::*;
#[test]
fn case_sensitive() {
let query = "duct";
let contents = "\
Rust:
safe, fast, productive.
Pick three.
Duct tape.";
assert_eq! (vec! ["safe, fast, productive."], search(query, contents));
}
#[test]
fn case_insensitive() {
let query = "rUst";
let contents = "\
Rust:
safe, fast, productive.
Pick three.
Trust me.";
assert_eq! (
vec! ["Rust:", "Trust me."],
search_insensitive(query, contents)
);
}