RustBook——Minigrep学习示例+注释

RustBook——Minigrep学习示例+注释

1. 代码

1.1 主程序代码 main.rs

use std::env;
use std::process;
use minigrep;
use minigrep::Config;

fn main() {

    let config = Config::new(env::args()).unwrap_or_else(|err| {
        eprintln!("Problem parsing arguments: {}", err);
        process::exit(1);
    });

    println!("Searching for '{}'", config.query);
    println!("In file '{}'", config.filename);

    if let Err(e) = minigrep::run(config) {
        eprintln!("Application error: {}", e);

        process::exit(1);
    }
}

1.2 库 lib.rs

  • 查询配置 Config
use std::env;

/// # 查询配置
/// - query: 待查询的字段
/// - filename: 待查询的文件
/// - case_sensitive: 是否大小写敏感
pub struct Config {
    pub query: String,
    pub filename: String,
    pub case_sensitive: bool,
}

impl Config {
    pub fn new(mut args: std::env::Args) -> Result<Config, &'static str> {
        // args是一个Iterator,一步一步的向后取数据

        // 第一个参数不要,是本程序的路径
        args.next();

        // 待查询的字段
        let query = match args.next() {
            Some(arg) => arg,
            None => return Err("Didn't get a query string"),
        };

        // 待检索的文档
        let filename = match args.next() {
            Some(arg) => arg,
            None => return Err("Didn't get a file name"),
        };

        // 1. 先检查参数中是否配置了大小写敏感
        // 2. 如果没配置,再看系统环境变量中是否配置了大小写敏感
        // 3. 如果都没配置,默认是大小写不敏感
        let case_sensitive = match args.next() {
            Some(arg) => arg == "CASE_SENSITIVE",
            None => env::var("CASE_SENSITIVE").is_ok(),
        };

        Ok(Config { query, filename, case_sensitive })
    }
}

  • 两个查询的方法(大小写敏感、不敏感)
/// # 大小写敏感查询
/// - query: 需要查询的字段
/// - contents: 需要查询的内容
fn search_case_sensitive<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
    // 获取contents的Iterator,进行过滤,保留包含query字段的行
    contents.lines()
        .filter(|line| line.contains(query))
        .collect()
}

/// # 大小写不敏感查询
/// - query: 需要查询的字段
/// - contents: 需要查询的内容
fn search_case_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()
}
  • run 方法 运行搜索
use std::error::Error;
use std::fs;

/// # 运行搜索
/// - config: 包括待查询的字段、待查询的文件、是否大小写敏感
pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
    // 读取文件内容,如果读取失败向外返回Err
    let contents = fs::read_to_string(config.filename)?;

    // 根据case_sensitive参数,决定走那种查询方式
    let results = if config.case_sensitive {
        search_case_sensitive(&config.query, &contents)
    } else {
        search_case_insensitive(&config.query, &contents)
    };

    // 打印查询结果
    for line in results {
        println!("---> {}", line);
    }

    Ok(())
}

2. 测试

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn case_sensitive() {
        let query = "duct";
        let contents = "\
Rust:
safe, fast, productive.
Pick three.";

        assert_eq!(vec!["safe, fast, productive."], search_case_sensitive(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_case_insensitive(query, contents));
    }
}
发布了128 篇原创文章 · 获赞 45 · 访问量 15万+

猜你喜欢

转载自blog.csdn.net/alionsss/article/details/90118200