当前位置: 首页 > news >正文

【Rust自学】12.2. 读取文件

12.2.0. 写在正文之前

第12章要做一个实例的项目——一个命令行程序。这个程序是一个grep(Global Regular Expression Print),是一个全局正则搜索和输出的工具。它的功能是在指定的文件中搜索出指定的文字。
请添加图片描述

这个项目分为这么几步:

  • 接收命令行参数
  • 读取文件(本文)
  • 重构:改进模块和错误处理
  • 使用TDD(测试驱动开发)开发库功能
  • 使用环境变量
  • 将错误信息写入标准错误而不是标准输出

喜欢的话别忘了点赞、收藏加关注哦(加关注即可阅读全文),对接下来的教程有兴趣的可以关注专栏。谢谢喵!(=・ω・=)

12.2.2. 回顾

这是截止到上文所写出的所有代码:

use std::env;  fn main() {  let args:Vec<String> = env::args().collect();  let query = &args[1];  let filename = &args[2];println!("search for {}", query);  println!("In file {}", filename);  
}

代码到此解决了读取用户指令的部分,接下来要根据用户的输入读取文件。

12.2.3. 读取文件

为了实现读取文件的操作,需要引入std::fs,这个模块可以处理与文件相关的事物:

use std::fs;

接下来,根据filename来读取文件:

let contents = fs::read_to_string(filename);

当然,读取会可能发生错误,所以它的返回值并不直接就是内容而是Result枚举,针对这个枚举,可以使用expect方法来解包,expect方法的参数是如果发生错误时打印的错误信息(expect方法在 9.2. Result枚举与可恢复的错误 Pt.1 中有详细介绍)。

let contents = fs::read_to_string(filename)
.expect("Somthing went wrong while reading the file");//这里换行只是为了这行不过长

如果能成功读取,就把读取到的内容打印出来:

println!("With text:\n{}", contents);

12.2.4. 代码测试

实现到这一步,可以对代码进行一些测试了。

这是截止到目前所写出的所有代码:

use std::env;  
use std::fs;  fn main() {  let args:Vec<String> = env::args().collect();  let query = &args[1];  let filename = &args[2];  println!("search for {}", query);  println!("In file {}", filename);  let contents = fs::read_to_string(filename)  .expect("Somthing went wrong while reading the file");//这里换行只是为了这行不过长  println!("With text:\n{}", contents);  
}

首先在项目目录下创建一个.txt文本,名字可以自己取,我取的是poem.txt,然后在里面随便放点文本内容,我放的是:

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!

然后输入命令:

cargo run -- the poem.txt
  • 这里的--是代表后面所写的是参数是用来区分Cargo 命令的参数传递给程序的参数的。它的作用是告诉 Cargo,接下来的内容不是 Cargo 的选项或参数,而是运行程序时需要传递给程序的参数。它并不会被读取和存储。
  • the对应的就是要查找的内容,会被存储在query
  • poem.txt就是文件名,会被存储在filename

输出:

$ cargo run -- the poem.txtCompiling minigrep v0.1.0 (file:///projects/minigrep)Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.0sRunning `target/debug/minigrep the poem.txt`
Searching for the
In file poem.txt
With text:
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!

没有任何问题。

http://www.lryc.cn/news/521061.html

相关文章:

  • C++内存泄露排查
  • Http 响应状态码 前后端联调
  • 48_Lua错误处理
  • shell脚本回顾1
  • 【3】管理无线控制器
  • SOME/IP 协议详解——服务发现
  • Flutter:封装ActionSheet 操作菜单
  • 力扣 全排列
  • Golang 设计模式
  • Matlab 具有周期性分布的死角孔的饱和空气多孔材料的声学特性
  • maven 项目怎么指定打包后名字
  • Java Web开发进阶——Spring Boot与Thymeleaf模板引擎
  • 论文笔记(四十七)Diffusion policy: Visuomotor policy learning via action diffusion(下)
  • 开始使用Panuon开源界面库环境配置并手写VS2019高仿界面
  • 新垂直电商的社交传播策略与AI智能名片2+1链动模式S2B2C商城小程序的应用探索
  • WPS计算机二级•表格函数计算
  • ESP32S3官方例程如何使用
  • 新版 MacOS 无法从 /usr/local/lib 加载动态链接库的解决办法
  • 【Varnish】:解决 Varnish 7.6 CDN 静态资源缓存失效问题
  • 【记录】篡改猴插件下载网页m3u8视频
  • PID控制器 (Proportional-Integral-Derivative Controller) 算法详解及案例分析
  • 【Java设计模式-5】装饰模式:给咖啡加点“佐料”
  • C++ using(八股总结)
  • 《分布式光纤传感:架设于桥梁监测领域的 “智慧光网” 》
  • C++(5)
  • 【进程与线程】程序和进程在内存中的表现
  • 个人主页搭建全流程(Nginx部署+SSL配置+DCDN加速)
  • 语音合成的预训练模型
  • 前端组件开发:组件开发 / 定义配置 / 配置驱动开发 / 爬虫配置 / 组件V2.0 / form表单 / table表单
  • Swagger生成Api文档的增强解决方案--knife4j