Rust: Reading and Writing Files

server/2024/10/19 6:28:06/

Reading and Writing Files

We need some way to actually get data from the filesystem so we can process it, and write it back when we’re done
我们需要某种方法从文件系统中实际获取数据,以便处理它,并在完成后将其写回来

rust">use std::fs;

std::fs::read_to_string returns a Result<String, std::io::Error>.
If the function succeeds, it produces a String. If it fails, it produces a std::io::Error, the standard library’s type for representing I/O problems.
std::fs::read_to_string返回Result<String, std::io::Error>。
如果函数成功,它将生成一个String。如果失败,它会产生std::io::Error,这是表示I/O问题的标准库类型。

rust">fn main() {let args = parse_args();let data = match fs::read_to_string(&args.filename) { Ok(v) => v,Err(e) => {eprintln!("{} failed to read from file '{}': {:?}","Error:".red().bold(), args.filename, e);std::process::exit(1);} };match fs::write(&args.output, &data) { Ok(_) => {},Err(e) => {eprintln!("{} failed to write to file '{}': {:?}","Error:".red().bold(), args.filename, e);std::process::exit(1);} };
}

Find and Replace

The final touch for this program is to implement its actual functionality: finding and replacing. For this, we’ll use the regex crate, which compiles and executes regular expressions. It provides a struct called Regex, which represents a compiled regular expression. Regex has a method replace_all, which does exactly what it says: it searches a string for all matches of the regular expression and replaces each one with a given replacement string. We can pull this logic out into a function:

这个程序的最后一步是实现它的实际功能:查找和替换。为此,我们将使用regex crate,它编译并执行正则表达式。它提供了一个名为Regex的结构体,它表示编译后的正则表达式。Regex有一个方法replace_all,它所做的正是它所说的:它在字符串中搜索正则表达式的所有匹配项,并用给定的替换字符串替换每个匹配项。我们可以把这个逻辑放到一个函数中:

rust">use regex::Regex;
fn replace(target: &str, replacement: &str, text: &str)-> Result<String, regex::Error>{let regex = Regex::new(target)?;Ok(regex.replace_all(text, replacement).to_string())}
rust">fn main() {let args = parse_args();let data = match fs::read_to_string(&args.filename) { Ok(v) => v,Err(e) => {eprintln!("{} failed to read from file '{}': {:?}","Error:".red().bold(), args.filename, e);std::process::exit(1);} };let replaced_data = match replace(&args.target, &args.replacement, &data) {Ok(v) => v,Err(e) => {eprintln!("{} failed to replace text: {:?}","Error:".red().bold(), e);std::process::exit(1);}};match fs::write(&args.output, &replaced_data) { Ok(v) => v,Err(e) => {eprintln!("{} failed to write to file '{}': {:?}","Error:".red().bold(), args.filename, e);std::process::exit(1);} };
}
rust">$ echo "Hello, world" > test.txt
$ cargo run "world" "Rust" test.txt test-modified.txt
$ cat test-modified.txt 
Hello, Rust

http://www.ppmy.cn/server/106970.html

相关文章

80、k8s概念及组件介绍

一、k8s kubernetes:k8s----希腊语&#xff0c;舵手&#xff0c;飞行员 1.1、k8s作用&#xff1a; ​ 用于自动部署&#xff0c;扩展&#xff0c;管理容器化部署的应用程序。开源&#xff08;半开源。&#xff09; ​ k8s的底层语言是由go语言。 ​ k8s理解成负责自动化运…

PostgreSQL 内核资源管理

在高负载环境下&#xff0c;尤其是在同一系统上运行多个 PostgreSQL 实例或在大型安装环境中&#xff0c;PostgreSQL 有时可能会耗尽操作系统的资源限制。本文介绍了 PostgreSQL 使用的关键内核资源&#xff0c;以及如何解决与这些资源消耗相关的问题。 19.4.1. 共享内存和信号…

小程序常用的模板语法

WXML 文件 <!-- page.wxml --> <view><!-- 数据绑定: 将数据 message 绑定到视图中 --><view>{{message}}</view><!-- 条件渲染: 根据 isLoggedIn 的值显示不同的内容 --><view wx:if"{{isLoggedIn}}">Welcome back!<…

内网横向移动常用方法

横向移动 #横向移动含义 横向移动是以已经被攻陷的系统为跳板&#xff0c;通过收集跳板机的信息&#xff08;文档&#xff0c;存储的凭证&#xff0c;ipc连接记录等等信息&#xff09;来访问其他域内主机。#常见横向手段 1&#xff0c;通过相同的用户名密码批量ipc连接其他域内…

Godot《躲避小兵》实战之为游戏添加音效

现在&#xff0c;我们已经完成了游戏的所有功能。以下是一些剩余的步骤&#xff0c;为游戏加点“料”&#xff0c;改善游戏体验。 随意用你自己的想法扩展游戏玩法。 背景 默认的灰色背景不是很吸引人&#xff0c;那么我们就来改一下颜色。一种方法是使用 ColorRect节点。将…

基于深度学习的材料性能预测

基于深度学习的材料性能预测是材料科学领域的一个前沿研究方向&#xff0c;它结合了人工智能和材料学&#xff0c;通过分析和建模复杂的材料数据&#xff0c;来预测材料的性能和特性。这一技术正在加速新材料的发现和优化过程&#xff0c;从而推动材料科学的发展。 1. 背景与动…

SpringCloudGateway重写负载均衡策略

背景 gateway中多实例请求转发&#xff0c;默认采用轮训转发策略。在有些场景下&#xff0c;某些请求想固定到某一台实例上&#xff0c;这里通过重写默认负载均衡策略的方式实现。 以下代码为&#xff0c;大文件分片上传&#xff0c;多实例场景&#xff0c;根据文件md5和实例…

英国政府停止使用人工智能

你是否注意到&#xff0c;每家公司都声称他们拥有一些新发现的人工智能技术&#xff0c;这些技术显然使他们更胜一筹&#xff0c;但这些人工智能却完全是空洞的&#xff0c;令人失望&#xff1f;我也是&#xff0c;这也是我对这项技术如此怀疑的一半原因。但在过去几年里&#…