学习Rust的第22天:mini_grep第2部分

ops/2024/9/23 6:34:10/

书接上文,在本文中,我们学习了如何通过将 Rust 程序的逻辑移至单独的库箱中并采用测试驱动开发 (TDD) 实践来重构 Rust 程序。通过在实现功能之前编写测试,我们确保了代码的可靠性。我们涵盖了基本的 Rust 概念,例如错误处理、环境变量和命令行参数。本文最后提出了最后一个改进:将错误消息重定向到 stderr 以提供更好的用户体验。

Recap 回顾

This is our code so far
这是我们到目前为止的代码

rust">use std::env;
use std::fs;
use std::process;
use std::error::Error;struct Config {query: String,file: String,
}impl Config{fn new(args: &[String]) -> Result<Config, &str>{if args.len() < 3{return Err("Not enough arguments.");}let query: String = args[1].clone();let file: String = args[2].clone();Ok(Config{query,file})}
}fn run(config: Config) -> Result<(), Box<dyn Error>>{let contents = fs::read_to_string(config.file)?;println!("file contents: {}",contents);Ok(())
}fn main(){let args: Vec<String> = env::args().collect();let config = Config::new(&args).unwrap_or_else(|err|{println!("Problem parsing arguments: {}",err);println!("Expected: {} search_query filename", args[0]);process::exit(1);});if let Err(e) = run(config) {println!("Application error: {}",e);process::exit(1);}
}

Explanation: 解释:

rust">use std::env;
use std::fs;
use std::process;
use std::error::Error;

These lines import specific modules from the standard library (std).
这些行从标准库 ( std ) 导入特定模块。

  • env: Provides functions for interacting with the environment (e.g., command-line arguments).
    env :提供与环境交互的函数(例如命令行参数)。
  • fs: Offers file system operations like reading and writing files.
    fs :提供文件系统操作,例如读取和写入文件。
  • process: Provides functions for interacting with processes (e.g., exiting a process).
    process :提供与进程交互的功能(例如,退出进程)。
  • error::Error: Imports the Error trait, which is used for error handling.
    error::Error :导入 Error 特征,用于错误处理。
rust">struct Config {query: String,file: String,
}
  • Defines a struct named Config with two fields: query and file, both of type String.
    定义一个名为 Config 的结构体,其中包含两个字段: query 和 file ,均为 String 类型。
rust">impl Config{fn new(args: &[String]) -> Result<Config, &str>{if args.len() < 3 {return Err("Not enough arguments.");}let query: String = args[1].clone();let file: String = args[2].clone();Ok(Config{query, file})}
}

Implements methods for the Config struct.
实现 Config 结构的方法。

  • Defines a constructor method new for creating a new Config instance.
    定义一个构造函数方法 new 用于创建新的 Config 实例。
  • Takes a slice of strings (&[String]) representing command-line arguments as input.
    将表示命令行参数的字符串片段 ( &[String] ) 作为输入。
  • Returns a Result where Ok contains a Config instance if arguments are sufficient, and Err contains an error message otherwise.
    如果参数足够,则返回 Result ,其中 Ok 包含 Config 实例,否则 Err 包含错误消息。
rust">fn run(config: Config) -> Result<(), Box<dyn Error>>{let contents = fs::read_to_string(config.file)?;println!("file contents: {}",contents);Ok(())
}

Defines a function run that takes a Config instance as input.
定义一个函数 run ,它将 Config 实例作为输入。

  • Attempts to read the contents of the file specified in the Config.
    尝试读取 Config 中指定的文件的内容。
  • Prints the contents of the file.
    打印文件的内容。
  • Returns Ok(()) if successful, indicating no error.
    如果成功则返回 Ok(()) ,表示没有错误。
rust">fn main(){let args: Vec<String> = env::args().collect();let config = Config::new(&args).unwrap_or_else(|err|{println!("Problem parsing arguments: {}",err);println!("Expected: {} search_query filename", args[0]);process::exit(1);});if let Err(e) = run(config) {println!("Application error: {}",e);process::exit(1);}
}

Defines the main function, the entry point of the program.
定义 main 函数,程序的入口点。

  • Retrieves command-line arguments and collects them into a vector of strings.
    检索命令行参数并将它们收集到字符串向量中。
  • Attempts to create a Config instance from the command-line arguments.
    尝试从命令行参数创建 Config 实例。
  • If successful, continues with the program execution.
    如果成功,则继续执行程序。
  • If unsuccessful, prints an error message and exits the program.
    如果不成功,则打印错误消息并退出程序。
  • Calls the run function with the Config instance.
    使用 Config 实例调用 run 函数。
  • Handles any errors that occur during the execution of run by printing an error message and exiting the program.
    通过打印错误消息并退出程序来处理 run 执行期间发生的任何错误。
rust">let config = Config::new(&args).unwrap_or_else(|err|{println!("Problem parsing arguments: {}",err);println!("Expected: {} search_query filename", args[0]);process::exit(1);});
  • This is a closure, an error-handling mechanism used when creating a Config instance from command-line arguments. It prints error details and expected usage if parsing fails, then exits the program with an error code. We will study about closures in detail in the upcoming articles
    这是一个闭包,是从命令行参数创建 Config 实例时使用的错误处理机制。如果解析失败,它会打印错误详细信息和预期用法,然后使用错误代码退出程序。我们将在接下来的文章中详细研究闭包

Extracting logic to a library crate
将逻辑提取到库箱中

Our main.rs file is kind of bloated at the moment and all oof our logic is stored in one place, to tackle this situation we can create a library crate and store all the logic there and our main.rs file can call the library crate for logic.
我们的 main.rs 文件目前有点臃肿,我们所有的逻辑都存储在一个地方,为了解决这种情况,我们可以创建一个库板条箱并将所有逻辑存储在那里,我们的 main.rs
First things first, create a lib.rs file in the src directory, this is where we will store all our logic…
首先,在 src 目录中创建一个 lib.rs 文件,这是我们存储所有逻辑的地方......

rust">$ touch src/lib.rs
rust">├── Cargo.lock
├── Cargo.toml
├── lorem.txt
├── src
│   ├── lib.rs
│   └── main.rs
└── target├── CACHEDIR.TAG└── debug

We will move our run() function and the Config struct and implementation to the lib.rs file with the relevant use statements
我们将把 run() 函数以及 Config 结构和实现移至 lib.rs 文件,并包含相关的 use 语句

rust">// lib.rsuse std::fs;
use std::error::Error;pub struct Config {pub query: String,pub file: String,
}impl Config{pub fn new(args: &[String]) -> Result<Config, &str>{if args.len() < 3{return Err("Not enough arguments.");}let query: String = args[1].clone();let file: String = args[2].clone();Ok(Config{query,file})}
}pub fn run(config: Config) -> Result<(), Box<dyn Error>>{let contents = fs::read_to_string(config.file)?;println!("file contents: {}",contents);Ok(())
}

Remember, everything in rust is private by default, that is where we can use the pub keyword to make it public
请记住,默认情况下,rust 中的所有内容都是私有的,这就是我们可以使用 pub 关键字将其公开的地方

Then we can import the Config struct from the library crate and call the run function from there as well in the main.rs file.
然后我们可以从库箱中导入 Config 结构,并在 main.rs 文件中调用 run 函数。

rust">use std::env;
use std::process;use minigrep::Config;fn main(){let args: Vec<String> = env::args().collect();let config = Config::new(&args).unwrap_or_else(|err|{println!("Problem parsing arguments: {}",err);println!("Expected: {} search_query filename", args[0]);process::exit(1);});if let Err(e) = minigrep::run(config) {println!("Application error: {}",e);process::exit(1);}
}

Now seems like a good time for us to dive into Test-driven development, this is an important concept when it comes to programming. It involves
现在似乎是我们深入研究测试驱动开发的好时机,这是编程时的一个重要概念。它涉及

  • Writing a test that fails
    编写失败的测试
  • Writing code to make it pass
    编写代码使其通过
  • Refactoring the code to make it readable
    重构代码以使其可读
  • REPEAT 重复

So let’s write a failing test
那么让我们编写一个失败的测试

rust">#[cfg(test)]
mod tests{#[test]fn single_result_test(){let query = "dolor";let contents = "Lorem
ipsum
dolor
sit
amet";assert_eq!(vec!["dolor"], search(query,contents));}
}

We have not yet created the search function, and running the test command now will result in a compilation error because of that…
我们还没有创建搜索功能,现在运行测试命令将导致编译错误,因为......

So let’s create a search function
那么让我们创建一个搜索功能

rust">pub fn search(query: &str, contents: &str) -> Vec<&str> {let mut results = Vec::new();for line in contents.lines(){if line.contains(query){results.push(line);}}results
}

As of now this function will give us an error to work with because out Vector is going to be tied to either query or contents and we haven’t specified the lifetime for it yet, so let’s do that
到目前为止,这个函数会给我们带来一个错误,因为 out Vector 将绑定到 query 或 contents 并且我们还没有指定它的生命周期,所以让我们这样做吧

rust">pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {let mut results = Vec::new();for line in contents.lines(){if line.contains(query){results.push(line);}}results
}

Running the test now, will pass
现在运行测试,将通过

rust">running 1 test
test tests::one_result ... oktest result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00sRunning unittests src/main.rs (target/debug/deps/minigrep-35c886545281e89c)running 0 teststest result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00sDoc-tests minigreprunning 0 teststest result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

Now that the search function works, lets use that in our run function
现在搜索功能可以工作了,让我们在运行功能中使用它

rust">pub fn run(config: Config) -> Result<(), Box<dyn Error>>{let contents = fs::read_to_string(config.file)?;let result = search(&config.query, &contents);for lines in result {println!("{}",lines);}Ok(())
}

Running this program we get
运行这个程序我们得到

rust">cargo run dolor lorem.txtdolor

It works, but there is one major problem here
它可以工作,但是这里有一个主要问题
Our search logic is case sensitive, Let’s fix it
我们的搜索逻辑区分大小写,让我们修复它

Continuing TDD, let’s make a failing test and then work our way up
继续 TDD,让我们进行一次失败的测试,然后继续努力

rust">#[test]
fn case_insensitive(){let query = "DoLoR";let contents = "Lorem
ipsum
dolor
sit
amet
DOLOR
DolOR
doLOR";assert_eq!(vec!["dolor","DOLOR","DolOR","doLOR"], search_case_insensitive(query,contents));}

This test will fail, because we don’t have the search_case_insensitive function as of now
这个测试将会失败,因为我们现在还没有 search_case_insensitive 函数

We will use most of the logic of the previous function here, and modify a few things
我们将在这里使用上一个函数的大部分逻辑,并修改一些内容

rust">pub fn search_case_insensitive<'a>(query: &str, contents: &'a str) -> Vec<&'a str>{let mut results = Vec::new();let query = query.to_lowercase();for line in contents.lines(){if line.to_lowercase().contains(&query){results
}

The difference in logic is as follows:
逻辑上的区别如下:

search_case_insensitive: 搜索不区分大小写:

  • This function converts both the query and each line of the content to lowercase using the to_lowercase() method. This ensures that the comparison is case insensitive.
    此函数使用 to_lowercase() 方法将查询和每一行内容转换为小写。这确保了比较不区分大小写。
  • After converting the query and the line to lowercase, it checks if the lowercase version of the line contains the lowercase version of the query.
    将查询和行转换为小写后,它检查该行的小写版本是否包含查询的小写版本。
  • If a match is found, it doesn’t actually push the original line into the results vector. Instead, it returns an empty results vector, thus indicating that it didn’t store the original lines matching the case-insensitive query. There’s a missing line to push the matched line into results.
    如果找到匹配项,它实际上不会将原始行推入结果向量中。相反,它返回一个空结果向量,从而表明它没有存储与不区分大小写的查询匹配的原始行。缺少一行将匹配行推入 results 。

search: 搜索:

  • This function searches for the query within each line of the content without altering the case.
    此函数在内容的每一行中搜索查询,而不改变大小写。
  • It iterates over each line of the content, and for each line, it checks if the line contains the query.
    它迭代内容的每一行,并针对每一行检查该行是否包含查询。
  • If a match is found, it pushes the original line (in its original case) into the results vector.
    如果找到匹配项,它将原始行(在其原始情况下)推送到结果向量中。
  • It doesn’t perform any case conversion, so the search is case sensitive by default.
    它不执行任何大小写转换,因此默认情况下搜索区分大小写。

Environment variables 环境变量

Now that we have two search functions, our run function needs to know which function to call, but to do that we will add a boolean to the Config struct named case_sensitive
现在我们有两个搜索函数,我们的 run 函数需要知道要调用哪个函数,但为此,我们将向名为 case_sensitive

rust">pub struct Config{pub query: String,pub file: String,pub case_sensitive: bool,
}

Then we will modify our run function to use the case_sensitive field…
然后我们将修改 run 函数以使用 case_sensitive 字段...

rust">pub fn run(config: Config) -> Result<(), Box<dyn Error>>{let contents = fs::read_to_string(config.file)?;let result = if config.case_sensitive{search(&config.query, &contents);}else {search_case_insensitive(&config.query, &contents);}for lines in result {println!("{}",lines);}Ok(())
}

Now we modify the new function to use environment variables for case_sensitivity
现在我们修改新函数以使用 case_sensitivity 的环境变量

We need to have the env module in scope to use this functionality
我们需要在范围内包含 env 模块才能使用此功能

rust">use std::env;
rust">impl Config{pub fn new(args: &[String]) -> Result<Config, &str>{if args.len() < 3{return Err("Not enough arguments.");}let query: String = args[1].clone();let file: String = args[2].clone();let case_sensitive = env::var("CASE_INSENSITIVE").is_err();Ok(Config{query,file,case_sensitive})}
}

Now for the final step we need to display our errors to stderr instead of stdout
现在,最后一步我们需要将错误显示到 stderr 而不是 stdout
All out error handling is in the main.rs file so let’s get to it. This step is really easy, We just need to change out println! for eprintln!this redirects the output to stderr. This is useful when a user wants to send the output stream to a file, they can still see the errors on their terminal screen.
所有错误处理都在 main.rs 文件中,所以让我们开始吧。这一步非常简单,我们只需要将 println! 更改为 eprintln! 这会将输出重定向到 stderr. 这在用户想要发送输出时很有用流到文件,他们仍然可以在终端屏幕上看到错误。

Congratulations on creating your very first useful Rust program
恭喜您创建了第一个有用的 Rust 程序

Directory structure : 目录结构:

rust">├── Cargo.lock
├── Cargo.toml
├── lorem.txt
├── src
│   ├── lib.rs
│   └── main.rs
└── target├── CACHEDIR.TAG└── debug

Code: 代码:

lib.rs 库文件

rust">use std::fs;
use std::error::Error;
use std::env;pub struct Config {pub query: String,pub file: String,pub case_sensitive: bool,
}impl Config{pub fn new(args: &[String]) -> Result<Config, &str>{if args.len() < 3{return Err("Not enough arguments.");}let query: String = args[1].clone();let file: String = args[2].clone();let case_sensitive = env::var("CASE_INSENSITIVE").is_err();Ok(Config{query,file,case_sensitive})}
}pub fn run(config: Config) -> Result<(), Box<dyn Error>>{let contents = fs::read_to_string(config.file)?;let result = if config.case_sensitive{search(&config.query, &contents)}else {search_case_insensitive(&config.query, &contents)};for lines in result {println!("{}",lines);}Ok(())
}pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str>{let mut results = Vec::new();for line in contents.lines() {if line.contains(query){results.push(line);}}results
}pub fn search_case_insensitive<'a>(query: &str, contents: &'a str) -> Vec<&'a str>{let mut results = Vec::new();let query = query.to_lowercase();for line in contents.lines(){if line.to_lowercase().contains(&query){results.push(line);}}results
}#[cfg(test)]
mod tests{use super::*;#[test]fn case_sensitive(){let query = "dolor";let contents = "Lorem
ipsum
dolor
DoloR
sit
amet";assert_eq!(vec!["dolor"], search(query,contents));}#[test]fn case_insensitive(){let query = "DoLoR";let contents = "Lorem
ipsum
dolor
sit
amet
DOLOR
DolOR
doLOR";assert_eq!(vec!["dolor","DOLOR","DolOR","doLOR"], search_case_insensitive(query,contents));}
}

main.rs 主程序.rs

rust">use std::env;
use std::process;use minigrep::Config;fn main(){let args: Vec<String> = env::args().collect();let config = Config::new(&args).unwrap_or_else(|err|{eprintln!("Problem parsing arguments: {}",err);eprintln!("Expected: {} search_query filename", args[0]);process::exit(1);});if let Err(e) = minigrep::run(config) {eprintln!("Application error: {}",e);process::exit(1);}
}

http://www.ppmy.cn/ops/29779.html

相关文章

初始计算机网络

TCP/IP TCP/IP模型 TCP/IP网络模型&#xff1a;对于不同设备之间的通信&#xff0c;就需要网络通信&#xff0c;而设备是多样性的&#xff0c;所以要兼容多种多样的设备&#xff0c;就协商出了一套通用的网络协议。 TCP/IP分层 这个网络协议是分层的&#xff0c;每一层都有…

万界星空科技商业开源MES+项目合作+商业开源低代码平台

今天我想和大家分享的是一套商业开源的 MES制造执行管理系统带源码。对于制造业而言&#xff0c;MES 是一个至关重要的系统&#xff0c;它可以帮助企业提高生产效率、优化资源利用、提高产品质量&#xff0c;从而增强市场竞争力。 什么是 MES&#xff1f; MES 是指通过计算机技…

一文理解前端如何调用后端(java)方法

阅读完文章大约需要3~5分钟 文章目录 一、什么是后端方法路径&#xff1f;二、ajax、axios调用后端方法总结 一、什么是后端方法路径&#xff1f; 这里针对的是 java 后端项目中在 controller 文件夹中的类文件&#xff0c;这类文件的后缀一般都会带有 controller&#xff0c…

CAE组件CEETRON SDK的应用价值与功能更新趋势

为什么要在应用程序中使用CAE组件&#xff1f; 常见的CAE工作流程涉及一系列阶段&#xff0c;所有阶段都需要复杂的专用工具才能产生有意义的结果。 此标准工作流程的设置阶段围绕为求解器提供生成有用的模拟所需的数据。为此&#xff0c;应用程序需要支持将CAD数据转换为…

二叉树遍历递归法迭代法实现

一.递归法实现二叉树遍历 前序遍历 创建一个节点类 属性是val,左节点&#xff0c;右节点 public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val x; } } 前序遍历 class Solution {public List<Integer> preorderTraversa…

OceanBase 分布式数据库【信创/国产化】- 登录 OceanBase 租户

本心、输入输出、结果 文章目录 OceanBase 分布式数据库【信创/国产化】- 登录 OceanBase 租户前言OceanBase 数据更新架构OceanBase 租户架构登录系统租户通过 MySQL 客户端登录通过 OBClient 登录登录最佳实践登录用户租户登录 Meta 租户OceanBase 分布式数据库【信创/国产化…

用HTML5实现播放gif文件

用HTML5实现播放gif文件 在HTML5中&#xff0c;你可以使用<img>标签来播放GIF文件。GIF文件本质上是一种图像格式&#xff0c;它支持动画效果&#xff0c;因此当在网页上加载时&#xff0c;它会自动播放动画。先看一个简单的示例&#xff1a; <!DOCTYPE html> &l…

从零开始:Django项目的创建与配置指南

title: 从零开始&#xff1a;Django项目的创建与配置指南 date: 2024/5/2 18:29:33 updated: 2024/5/2 18:29:33 categories: 后端开发 tags: DjangoWebDevPythonORMSecurityDeploymentOptimization Django简介&#xff1a; Django是一个开源的高级Python Web框架&#xff…