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

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

AI generated image AI生成图像

Recap 回顾

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

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: 解释:

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 特征,用于错误处理。
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 类型。
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 包含错误消息。
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(()) ,表示没有错误。
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 执行期间发生的任何错误。
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 文件,这是我们存储所有逻辑的地方......

$ touch src/lib.rs
├── 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 语句

// lib.rs

use 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 函数。

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
那么让我们编写一个失败的测试

#[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
那么让我们创建一个搜索功能

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 并且我们还没有指定它的生命周期,所以让我们这样做吧

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
现在运行测试,将通过

running 1 test
test tests::one_result ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

     Running unittests src/main.rs (target/debug/deps/minigrep-35c886545281e89c)

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

   Doc-tests minigrep

running 0 tests

test 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
现在搜索功能可以工作了,让我们在运行功能中使用它

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
运行这个程序我们得到

cargo run dolor lorem.txt

dolor

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,让我们进行一次失败的测试,然后继续努力

#[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
我们将在这里使用上一个函数的大部分逻辑,并修改一些内容

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

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 字段...

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 模块才能使用此功能

use std::env;
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 : 目录结构:

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

Code: 代码:

lib.rs 库文件

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

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.mfbz.cn/a/583975.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

小程序SSL证书更新指南

随着网络技术的不断发展&#xff0c;小程序已经成为许多企业和个人进行业务推广和服务提供的重要平台。在享受小程序带来的便利和高效的同时&#xff0c;我们也必须重视其安全性问题。SSL证书作为保障小程序数据传输安全的重要手段&#xff0c;其更新工作不容忽视。本文将为大家…

在线教程|零门槛部署 Llama 3,70B 版本只占 1.07G 存储空间,新用户免费体验 8B 版本

4 月 18 日&#xff0c;Meta 宣布开源 Llama 3&#xff0c;这个号称「迄今为止最好的开源大模型」一经发布&#xff0c;立刻引爆科技圈&#xff01; 发布当天恰逢斯坦福大学教授、AI 顶尖专家吴恩达的生日&#xff0c;作为 AI 开源倡导者&#xff0c;他激动地发文表示&#xff…

CogAgent:开创性的VLM在GUI理解和自动化任务中的突破

尽管LLMs如ChatGPT在撰写电子邮件等任务上能够提供帮助&#xff0c;它们在理解和与GUIs交互方面存在挑战&#xff0c;这限制了它们在提高自动化水平方面的潜力。数字世界中的自主代理是许多现代人梦寐以求的理想助手。这些代理能够根据用户输入的任务描述自动完成如在线预订票务…

【doghead】ubuntu构建libuv

按照官方的文档2024年3月的版本。首先构建libuv 最终构建的还得了test 构建过程 zhangbin@DESKTOP-1723CM1:/mnt/d/XTRANS/thunderbolt/ayame/zhb-bifrost$ ls Bifrost-202403 README.md draw player-only worker 大神的带宽估计.png zhangbin@DESKTOP-1723CM1:/mnt/d/XTRANS/…

计算机网络 备查

OSI 七层模型 七层模型协议各层实现的功能 简要 详细 TCP/IP协议 组成 1.传输层协议 TCP 2.网络层协议 IP 协议数据单元&#xff08;PDU&#xff09;和 封装 数据收发过程 数据发送过程 1. 2.终端用户生成数据 3.数据被分段&#xff0c;并加上TCP头 4.网络层添加IP地址信息…

React + 项目(从基础到实战) -- 第十期

目标 学会react 状态管理工具 使用redux管理用户状态 Context 跨层级传递,不像props层层传递类似于Vue的provide/inject用于:切换主题颜色,切换语言 useReducer useState 的替代方案 简化版的redux MobX 1. MobX 介绍 MobX 中文文档 声明式的修改数据 , 像vue state ac…

【算法基础实验】图论-UnionFind连通性检测之quick-union

Union-Find连通性检测之quick-union 理论基础 在图论和计算机科学中&#xff0c;Union-Find 或并查集是一种用于处理一组元素分成的多个不相交集合&#xff08;即连通分量&#xff09;的情况&#xff0c;并能快速回答这组元素中任意两个元素是否在同一集合中的问题。Union-Fi…

55.基于SpringBoot + Vue实现的前后端分离-旅游管理系统(项目 + 论文)

项目介绍 本站是一个B/S模式系统&#xff0c;采用SpringBoot Vue框架&#xff0c;MYSQL数据库设计开发&#xff0c;充分保证系统的稳定性。系统具有界面清晰、操作简单&#xff0c;功能齐全的特点&#xff0c;使得基于SpringBoot Vue技术的旅游管理系统设计与实现管理工作系统…

【Node.js工程师养成计划】之express框架

一、Express 官网&#xff1a;http://www.expressjs.com.cn express 是一个基于内置核心 http 模块的&#xff0c;一个第三方的包&#xff0c;专注于 web 服务器的构建。 Express 是一个简洁而灵活的 node.js Web应用框架, 提供了一系列强大特性帮助你创建各种 Web 应用&…

docker学习笔记3:VmWare CentOS7安装与静态ip配置

文章目录 一、安装CentOS71、下载centos镜像2、安装二、设置静态ip三、xshell连接centos本专栏的docker环境是在centos7里安装,因此首先需要会安装centos虚拟机。 本篇博客介绍如何在vm虚拟机里安装centos7。 一、安装CentOS7 1、下载centos镜像 推荐清华源,下载如下版本 …

使用量排名前50的GPTs趋势和特征

Chatgpt的gpt商店已经有几千gpts了。目前哪些gpts比较受欢迎呢&#xff1f;有哪些趋势和投资呢? 根据whatplugin.ai&#xff08;截止日期为2024年3月&#xff09;&#xff0c;使用量最多的50个gpts数据分析结果如下&#xff1a; GPTs类型的分布情况如下&#xff1a; 图像生成…

案例-部门管理-删除

黑马程序员JavaWeb开发教程 文章目录 一、查看页面原型二、查看接口文档三、开发1、Controller2、Service&#xff08;1&#xff09;service接口层&#xff08;3&#xff09;service实现层 3、Mapper4、Postman 一、查看页面原型 二、查看接口文档 三、开发 1、Controller 因…

Keepalived+LVS实现Nginx集群配置

Nginx1和Nginx2组成集群&#xff0c;为了实现负载均衡&#xff0c;在集群的前端配置了LVS服务&#xff0c;但是一台LVS容器产生单点故障&#xff0c;因此需要过Keepalived实现LVS的高可用集群 192.168.136.55node1keepalived192.168.136.56node2keeplived192.168.136.57 node3n…

Excel 中用于在一个范围中查找特定的值,并返回同一行中指定列的值 顺序不一样 可以处理吗

一、需求 Excel 中&#xff0c;在一列&#xff08;某范围内&#xff09;查找另一列特定的值&#xff0c;并返回同一行中另一指定列的值&#xff0c; 查找列和返回列的顺序不一样 二、 实现 1、下面是一个使用 INDEX 和 MATCH 函数的例子&#xff1a; 假设你有以下数据&…

CI/CD:基于kubernetes的Gitlab搭建

1. 项目目标 &#xff08;1&#xff09;熟悉使用k8s环境搭建Gitlab &#xff08;2&#xff09;熟练应用Gitlab基本配置 2. 项目准备 2.1. 规划节点 主机名 主机IP 节点规划 k8s-master 10.0.1.1 kube_master k8s-node1 10.0.1.2 kube_node k8s-node2 10.0.1.3 k…

影响外汇交易盈利的因素有哪些?

外汇交易就是通过汇率的差价来赚取相应的利润。在外汇交易中&#xff0c;投资者是否可以盈利&#xff0c;主要取决于是否正确的判断了市场趋势和行情。投资者在交易过程中受到主观和客观的因素影响&#xff0c;具体包含这些内容。 影响外汇交易盈利的因素有哪些&#xff1f; 1、…

【酱浦菌-爬虫项目】爬取学术堂论文信息

1. 首先&#xff0c;代码定义了一个名为 url 的变量&#xff0c;它是一个包含三个网址的集合&#xff08;或者说是一个集合的字典&#xff09;。这些网址分别是&#xff1a; - ‘http://www.xueshut.com/lwtimu/127966.html’ - ‘http://www.xueshut.com/lwtimu/12…

nmap扫描工控设备的脚本支持

参考资料 转自&#xff08;http://www.360doc.com/content/15/1201/11/26186435_517125254.shtml&#xff09; 介绍 NMAP是一款强大的网络扫描工具&#xff0c;除了普通的TCP/IP网络扫描之外&#xff0c;NMAP的扩展脚本功能为我们提供了更为广阔的应用范围。 针对脚本学习可…

Python使用设计模式中的建筑模式将数据写入Excel且满足条件内容标红

对于这个任务&#xff0c;适合使用"Builder"设计模式。Builder模式的主要目的是将对象的构建与其表示分离&#xff0c;以便相同的构建过程可以创建不同的表示。在这个情况下&#xff0c;我们需要一个构建器来逐行构建Excel表格&#xff0c;并根据给定的数据添加相应的…

C++中auto关键字的用法详解

1.简介 auto作为一个C语言就存在的关键字&#xff0c;在C语言和C之间却有很大区别。 在C语言中auto修饰的变量&#xff0c;是具有自动存储器的局部变量&#xff0c;但因为局部变量默认类别默认是auto修饰导致一直没有人去使用它。 C11中&#xff0c;标准委员会赋予了auto全新…