学习Rust的第26天:Rust中的cp

2024-05-04 11:20
文章标签 rust 学习 26 cp

本文主要是介绍学习Rust的第26天:Rust中的cp,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在本文中复刻了 cp 实用程序的功能,我想默认使其递归,因为每次我想复制时都输入 -R 文件夹都会觉得有点重复,本文代码将与前文代码保持相似,我们只会更改程序的核心功能和一些变量名称以匹配用例

Pseudo Code 伪代码

function copy(src,dst)->result{create dst directoryfor entry in src{get file_typeif file_type.is_dir(){copy(entry.path(), dst.as_ref().join(entry.file_name()))?} else {fs::copy(entry.path(), dst.as_ref().join(entry.file_name()))?}}
}args = command_line_arguments
remove the first element of the args vector
if args.length == 0 {print error_message
}
else if args.contains("--help") {print help_message
} else {result = copy(args[0],args[1])error handling
}

Looks pretty simple, we’ll create a lib.rs and do the basic setup tasks just like yesterday. We’ll create the following :
看起来很简单,我们将创建一个 lib.rs 并像昨天一样执行基本设置任务。我们将创建以下内容:

  1. config Struct  config 结构体
  2. An implementation block 一个实现块
  3. new method  new 方法
  4. help method  help 方法
  5. not_enough_arguments method  not_enough_arguments 方法
  6. run function  run 函数

and then we’ll write the logic for our copy function
然后我们将为 copy 函数编写逻辑

Creating a new cargo project and setting up the lib.rs file
创建一个新的 Cargo 项目并设置 lib.rs 文件

use std::path::Path; 
use std::{fs, io};pub struct Config<'a> {pub files: &'a Vec<String>,
}impl Config<'_> {pub fn new(args: &Vec<String>) -> Config {Config { files: args }}fn not_enough_arguments() {eprintln!("cp: missing operand");eprintln!("For help use: cp --help");}fn help() {eprintln!("This is a cheap little clone of the cp utility in the GNU core utilities, the catch is that I made it in rust, check out more of my work at medium: https://shafinmurani.medium.com");eprintln!("This is recursive by default so dont worry about anything, just run it :D");eprintln!("To use this util: cp source destination/folder_name");}pub fn run(&self) {if self.files.len() == 0 {Self::not_enough_arguments();} else if self.files.contains(&String::from("--help")) {Self::help();} else {// copy_function}}
}
  1. use std::path::Path;: This imports the Path struct from the std::path module. The Path struct represents file system paths and is used for manipulating and working with file paths.
    use std::path::Path; :这会从 std::path 模块导入 Path 结构。 Path 结构表示文件系统路径,用于操作和使用文件路径。
  2. use std::{fs, io};: This imports the fs module and the io module from the standard library. These modules provide functionalities related to file system operations (fs module) and input/output (io module).
    use std::{fs, io}; :这会从标准库导入 fs 模块和 io 模块。这些模块提供与文件系统操作( fs 模块)和输入/输出( io 模块)相关的功能。

That’s all the setup tasks we need to do, now to creating a copy function,
这就是我们需要做的所有设置任务,现在创建一个复制功能,

Let’s first understand our requirements:
我们先来了解一下我们的需求:

This function, copy_dir_all, is going to be responsible for recursively copying the contents of a directory from a source location to a destination location.
此函数 copy_dir_all 将负责将目录内容从源位置递归复制到目标位置。
This function will take two arguments: src and dst
该函数将采用两个参数: src 和 dst

fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> io::Result<()> {fs::create_dir_all(&dst)?;for entry in fs::read_dir(src)? {let entry = entry?;let ty = entry.file_type()?;if ty.is_dir() {Self::copy_dir_all(entry.path(), dst.as_ref().join(entry.file_name()))?;} else {fs::copy(entry.path(), dst.as_ref().join(entry.file_name()))?;}}Ok(())}

This function takes two arguments: src and dst, both of which must implement the AsRef<Path> trait. AsRef<Path> is a trait used for types that can be converted into a Path. This allows flexibility in accepting different types as source and destination paths.
此函数采用两个参数: src 和 dst ,两者都必须实现 AsRef<Path> 特征。 AsRef<Path> 是用于可以转换为 Path 的类型的特征。这允许灵活地接受不同类型作为源路径和目标路径。

fs::create_dir_all(&dst)?;

This line attempts to create the destination directory recursively using fs::create_dir_all. If the directory already exists, it will not raise an error. &dst is used to pass a reference to the destination path.
此行尝试使用 fs::create_dir_all 递归创建目标目录。如果该目录已经存在,则不会引发错误。 &dst 用于传递对目标路径的引用。

for entry in fs::read_dir(src)? {

This line iterates over the entries (files and directories) in the source directory using fs::read_dir. The read_dir function returns an iterator over the entries in a directory.
此行使用 fs::read_dir 迭代源目录中的条目(文件和目录)。 read_dir 函数返回目录中条目的迭代器。

let entry = entry?;

This line unwraps the result of iterating over the directory entries, handling any potential errors that may occur.
此行解开目录条目迭代的结果,处理可能发生的任何潜在错误。

let ty = entry.file_type()?;

This line obtains the file type of the current entry. file_type returns a FileType representing the type of the file, which can be a directory, file, symbolic link, etc.
该行获取当前条目的文件类型。 file_type 返回表示文件类型的 FileType ,可以是目录、文件、符号链接等。

if ty.is_dir() {

This condition checks if the current entry is a directory.
此条件检查当前条目是否是目录。

Self::copy_dir_all(entry.path(), dst.as_ref().join(entry.file_name()))?;

If the current entry is a directory, the function recursively calls itself (copy_dir_all) with the path of the subdirectory as the new source and the destination joined with the current entry's name.
如果当前条目是目录,则该函数递归调用自身 ( copy_dir_all ),并将子目录的路径作为新的源,并将目标与当前条目的名称连接起来。

} else {
fs::copy(entry.path(), dst.as_ref().join(entry.file_name()))?;

If the current entry is not a directory (i.e., it's a file), this block is executed.
如果当前条目不是目录(即,它是文件),则执行此块。

This line copies the file from the source path to the destination path using fs::copy.
此行使用 fs::copy 将文件从源路径复制到目标路径。
The loop continues until all entries in the source directory have been processed.
循环继续,直到源目录中的所有条目都已处理完毕。
If the function executes without any errors, it returns Ok(()), indicating success. This ends the function definition.
如果函数执行没有任何错误,则返回 Ok(()) ,表示成功。函数定义到此结束。

Let’s implement this in our run function,
让我们在 run 函数中实现它,

pub fn run(&self) {if self.files.len() == 0 {Self::not_enough_arguments();} else if self.files.contains(&String::from("--help")) {Self::help();} else {let result = Self::copy_dir_all(self.files[0].clone(), self.files[1].clone());match result {Ok(()) => {}Err(e) => {eprintln!("Application error: {}", e);}};}}

If there are no arguments provided:
如果没有提供参数:

  • Print an error message indicating not enough arguments.
    打印一条错误消息,指示参数不足。

Else if the --help argument is present:
否则,如果存在 --help 参数:

  • Print a help message explaining how to use the utility.
    打印一条帮助消息,解释如何使用该实用程序。

Otherwise: 否则:

  • Attempt to copy the contents of the source directory to the destination directory.
    尝试将源目录的内容复制到目标目录。
  • If successful, do nothing.
    如果成功,则不执行任何操作。

If an error occurs: 如果发生错误:

  • Print an error message containing the specific error encountered.
    打印包含遇到的特定错误的错误消息。

Now the main.rs file 现在是 main.rs 文件

We’ll import the Config struct, get the command line arguments, call the new function on the Config struct and run it, and hopefully it’ll work :D
我们将导入 Config 结构,获取命令行参数,在 Config 结构上调用 new 函数并运行它,希望它能工作:D

use cp::Config;
use std::env;
fn main() {let mut args: Vec<String> = env::args().collect();args.remove(0);let config = Config::new(&args);config.run();
}

Looks good? 看起来不错?

Practical 实际的

Thats the tree of the /tmp directory on my docker container
这是我的 docker 容器上 /tmp 目录的树

.
└── test├── test2 // we will copy this folder out to the /tmp directory│   └── text_file2.txt└── text_file.txt

Running this command : 运行这个命令:

# ~/learning_rust/linux_tools/cp/target/debug/cp test/test2/ ./test2#BINARY                                        #SRC         #DESTINATION

and now taking a look at the tree of the /tmp dir I get the following :
现在看看 /tmp 目录的树,我得到以下信息:

.
├── test
│   ├── test2
│   │   └── text_file2.txt
│   └── text_file.txt
└── test2└── text_file2.txt

So, it does work :D
所以,它确实有效:D

这篇关于学习Rust的第26天:Rust中的cp的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/959080

相关文章

51单片机学习记录———定时器

文章目录 前言一、定时器介绍二、STC89C52定时器资源三、定时器框图四、定时器模式五、定时器相关寄存器六、定时器练习 前言 一个学习嵌入式的小白~ 有问题评论区或私信指出~ 提示:以下是本篇文章正文内容,下面案例可供参考 一、定时器介绍 定时器介绍:51单片机的定时器属于单片机的内部资源,其电路的连接和运转均在单片机内部完成。 定时器作用: 1.用于计数系统,可

问题:第一次世界大战的起止时间是 #其他#学习方法#微信

问题:第一次世界大战的起止时间是 A.1913 ~1918 年 B.1913 ~1918 年 C.1914 ~1918 年 D.1914 ~1919 年 参考答案如图所示

[word] word设置上标快捷键 #学习方法#其他#媒体

word设置上标快捷键 办公中,少不了使用word,这个是大家必备的软件,今天给大家分享word设置上标快捷键,希望在办公中能帮到您! 1、添加上标 在录入一些公式,或者是化学产品时,需要添加上标内容,按下快捷键Ctrl+shift++就能将需要的内容设置为上标符号。 word设置上标快捷键的方法就是以上内容了,需要的小伙伴都可以试一试呢!

AssetBundle学习笔记

AssetBundle是unity自定义的资源格式,通过调用引擎的资源打包接口对资源进行打包成.assetbundle格式的资源包。本文介绍了AssetBundle的生成,使用,加载,卸载以及Unity资源更新的一个基本步骤。 目录 1.定义: 2.AssetBundle的生成: 1)设置AssetBundle包的属性——通过编辑器界面 补充:分组策略 2)调用引擎接口API

Javascript高级程序设计(第四版)--学习记录之变量、内存

原始值与引用值 原始值:简单的数据即基础数据类型,按值访问。 引用值:由多个值构成的对象即复杂数据类型,按引用访问。 动态属性 对于引用值而言,可以随时添加、修改和删除其属性和方法。 let person = new Object();person.name = 'Jason';person.age = 42;console.log(person.name,person.age);//'J

大学湖北中医药大学法医学试题及答案,分享几个实用搜题和学习工具 #微信#学习方法#职场发展

今天分享拥有拍照搜题、文字搜题、语音搜题、多重搜题等搜题模式,可以快速查找问题解析,加深对题目答案的理解。 1.快练题 这是一个网站 找题的网站海量题库,在线搜题,快速刷题~为您提供百万优质题库,直接搜索题库名称,支持多种刷题模式:顺序练习、语音听题、本地搜题、顺序阅读、模拟考试、组卷考试、赶快下载吧! 2.彩虹搜题 这是个老公众号了 支持手写输入,截图搜题,详细步骤,解题必备

《offer来了》第二章学习笔记

1.集合 Java四种集合:List、Queue、Set和Map 1.1.List:可重复 有序的Collection ArrayList: 基于数组实现,增删慢,查询快,线程不安全 Vector: 基于数组实现,增删慢,查询快,线程安全 LinkedList: 基于双向链实现,增删快,查询慢,线程不安全 1.2.Queue:队列 ArrayBlockingQueue:

硬件基础知识——自学习梳理

计算机存储分为闪存和永久性存储。 硬盘(永久存储)主要分为机械磁盘和固态硬盘。 机械磁盘主要靠磁颗粒的正负极方向来存储0或1,且机械磁盘没有使用寿命。 固态硬盘就有使用寿命了,大概支持30w次的读写操作。 闪存使用的是电容进行存储,断电数据就没了。 器件之间传输bit数据在总线上是一个一个传输的,因为通过电压传输(电流不稳定),但是电压属于电势能,所以可以叠加互相干扰,这也就是硬盘,U盘

人工智能机器学习算法总结神经网络算法(前向及反向传播)

1.定义,意义和优缺点 定义: 神经网络算法是一种模仿人类大脑神经元之间连接方式的机器学习算法。通过多层神经元的组合和激活函数的非线性转换,神经网络能够学习数据的特征和模式,实现对复杂数据的建模和预测。(我们可以借助人类的神经元模型来更好的帮助我们理解该算法的本质,不过这里需要说明的是,虽然名字是神经网络,并且结构等等也是借鉴了神经网络,但其原型以及算法本质上还和生物层面的神经网络运行原理存在

Python应用开发——30天学习Streamlit Python包进行APP的构建(9)

st.area_chart 显示区域图。 这是围绕 st.altair_chart 的语法糖。主要区别在于该命令使用数据自身的列和指数来计算图表的 Altair 规格。因此,在许多 "只需绘制此图 "的情况下,该命令更易于使用,但可定制性较差。 如果 st.area_chart 无法正确猜测数据规格,请尝试使用 st.altair_chart 指定所需的图表。 Function signa