学习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

相关文章

苹果macOS 26 Tahoe主题功能大升级:可定制图标/高亮文本/文件夹颜色

《苹果macOS26Tahoe主题功能大升级:可定制图标/高亮文本/文件夹颜色》在整体系统设计方面,macOS26采用了全新的玻璃质感视觉风格,应用于Dock栏、应用图标以及桌面小部件等多个界面... 科技媒体 MACRumors 昨日(6 月 13 日)发布博文,报道称在 macOS 26 Tahoe 中

Go学习记录之runtime包深入解析

《Go学习记录之runtime包深入解析》Go语言runtime包管理运行时环境,涵盖goroutine调度、内存分配、垃圾回收、类型信息等核心功能,:本文主要介绍Go学习记录之runtime包的... 目录前言:一、runtime包内容学习1、作用:① Goroutine和并发控制:② 垃圾回收:③ 栈和

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

重新对Java的类加载器的学习方式

《重新对Java的类加载器的学习方式》:本文主要介绍重新对Java的类加载器的学习方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、介绍1.1、简介1.2、符号引用和直接引用1、符号引用2、直接引用3、符号转直接的过程2、加载流程3、类加载的分类3.1、显示

rust 中的 EBNF简介举例

《rust中的EBNF简介举例》:本文主要介绍rust中的EBNF简介举例,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. 什么是 EBNF?2. 核心概念3. EBNF 语法符号详解4. 如何阅读 EBNF 规则5. 示例示例 1:简单的电子邮件地址

Linux搭建单机MySQL8.0.26版本的操作方法

《Linux搭建单机MySQL8.0.26版本的操作方法》:本文主要介绍Linux搭建单机MySQL8.0.26版本的操作方法,本文通过图文并茂的形式给大家讲解的非常详细,感兴趣的朋友一起看看吧... 目录概述环境信息数据库服务安装步骤下载前置依赖服务下载方式一:进入官网下载,并上传到宿主机中,适合离线环境

Java学习手册之Filter和Listener使用方法

《Java学习手册之Filter和Listener使用方法》:本文主要介绍Java学习手册之Filter和Listener使用方法的相关资料,Filter是一种拦截器,可以在请求到达Servl... 目录一、Filter(过滤器)1. Filter 的工作原理2. Filter 的配置与使用二、Listen

Java进阶学习之如何开启远程调式

《Java进阶学习之如何开启远程调式》Java开发中的远程调试是一项至关重要的技能,特别是在处理生产环境的问题或者协作开发时,:本文主要介绍Java进阶学习之如何开启远程调式的相关资料,需要的朋友... 目录概述Java远程调试的开启与底层原理开启Java远程调试底层原理JVM参数总结&nbsMbKKXJx

Rust中的注释使用解读

《Rust中的注释使用解读》本文介绍了Rust中的行注释、块注释和文档注释的使用方法,通过示例展示了如何在实际代码中应用这些注释,以提高代码的可读性和可维护性... 目录Rust 中的注释使用指南1. 行注释示例:行注释2. 块注释示例:块注释3. 文档注释示例:文档注释4. 综合示例总结Rust 中的注释

Rust格式化输出方式总结

《Rust格式化输出方式总结》Rust提供了强大的格式化输出功能,通过std::fmt模块和相关的宏来实现,主要的输出宏包括println!和format!,它们支持多种格式化占位符,如{}、{:?}... 目录Rust格式化输出方式基本的格式化输出格式化占位符Format 特性总结Rust格式化输出方式