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

news2024/10/5 23:29:12

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

Pseudo Code 伪代码

function copy(src,dst)->result{
  create dst directory
  for entry in src{
    get file_type
    if 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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1642707.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

STM32G474 CMAKE VSCODE 开发环境搭建

本篇博文尝试搭建 stm32g474 的开发环境 一. 工具安装 1. 关于 MinGW、OpenOCD、Zadig 这些工具的下载和安装见 JlinkOpenOCDSTM32 Vscode 下载和调试环境搭建_vscode openocd stm32 jlink-CSDN博客 2. 导出一个 STM32 的 CMAKE 工程&#xff0c;这里略过。 3. 安装 ninja …

C++:继承-继承权限

在C中&#xff0c;类的权限分为公有、私有和保护三种。这些权限控制了类的成员&#xff08;数据成员和成员函数&#xff09;对外部代码的可见性和访问性。 公有&#xff08;public&#xff09;权限&#xff1a; 在公有权限下声明的成员可以被类的外部代码直接访问&#xff1b;公…

小程序引入 Vant Weapp 极简教程

一切以 Vant Weapp 官方文档 为准 Vant Weapp 官方文档 - 快速入手 1. 安装nodejs 前往官网下载安装即可 nodejs官网 安装好后 在命令行&#xff08;winr&#xff0c;输入cmd&#xff09;输入 node -v若显示版本信息&#xff0c;即为安装成功 2. 在 小程序根目录 命令行/终端…

langchain+qwen1.5-7b-chat搭建本地RAG系统

已开源&#xff1a;https://github.com/stay-leave/enhance_llm 概念 检索增强生成&#xff08;Retrieval Augmented Generation, RAG&#xff09;是一种结合语言模型和信息检索的技术&#xff0c;用于生成更准确且与上下文相关的输出。 通用模型遇到的问题&#xff0c;也是…

头歌实践教学平台:三维图形观察OpenGL1.0

一.任务描述 根据提示&#xff0c;在右侧修改代码&#xff0c;并自己绘制出图形。平台会对你编写的代码进行测试。 1.本关任务 学习了解三维图形几何变换原理。 理解掌握OpenGL三维图形几何变换的方法。 理解掌握OpenGL程序的模型视图变换。 掌握OpenGL三维图形显示与观察的…

怎么用CAPL与Python交互

怎么用CAPL与其他应用程序交互 怎么用CAPL与Python交互 怎么用CAPL与Python交互 怎么用CAPL与其他应用程序交互前言1、CAPL怎么调Python&#xff1f;1.1CAPL调Python的命令1.2CAPL调用Python实例 2、怎么把python运行的结果返回给CAPL2.1通过环境变量 3、CAPL调Python的输入参…

OCC笔记:选择TopoDS_Shape顶点、边、面等等

1、通过AIS_InteractiveContext的函数访问当前选择的图形 hAISContext->InitSelected(); hAISContext->MoreSelected(); hAISContext->NextSelected()&#xff1b; hAISContext->SelectedShape()&#xff1b; 其中hAISContext->SelectedShape()通过StdSelect_…

C语言——rand函数

一、rand函数 这是一个在 C 标准库 <stdlib.h> 中定义的函数&#xff0c;用于生成伪随机数&#xff0c;默认情况下&#xff0c;它生成从 0 到 RAND_MAX 的伪随机数&#xff0c;其中 RAND_MAX 是一个常数&#xff0c;通常是 32767。 1、函数原型&#xff1a; 2、函数返回…

MongoDB的分片集群

MongoDB分片技术 介绍 ​ 分片&#xff08;sharding&#xff09;是MongoDB用来将大型集合分割到不同服务器上采用的方法。分片这种说法起源于关系型数据库。但是实际上非关系型数据库在分片方面相比于传统的关系型数据库更有优势。 ​ 与MySQL分库方案对比&#xff0c;MongoDB…

my-room-in-3d中的电脑,电视,桌面光带发光原理

1. my-room-in-3d中的电脑&#xff0c;电视&#xff0c;桌面光带发光原理 最近在github中&#xff0c;看到了这样的一个项目&#xff1b; 项目地址 我看到的时候&#xff0c;蛮好奇他这个光带时怎么做的。 最后发现&#xff0c;他是通过&#xff0c;加载一个 lightMap.jpg这个…

分布式与一致性协议之一致哈希算法(二)

一致哈希算法 使用哈希算法有什么问题 通过哈希算法&#xff0c;每个key都可以寻址到对应的服务器&#xff0c;比如&#xff0c;查询key是key-01,计算公式为hash(key-01)%3,警告过计算寻址到了编号为1的服务器节点A&#xff0c;如图所示。 但如果服务器数量发生变化&#x…

分享一篇关于AGI的短文:苦涩的教训

学习强化学习之父、加拿大计算机科学家理查德萨顿&#xff08; Richard S. Sutton &#xff09;2019年的经典文章《The Bitter Lesson&#xff08;苦涩的教训&#xff09;》。 文章指出&#xff0c;过去70年来AI研究走过的最大弯路&#xff0c;就是过于重视人类既有经验和知识&…

STM32控制DS1302时钟模块获取实时时间

时间记录&#xff1a;2024/3/30 一、知识点 &#xff08;1&#xff09;读写数据时序&#xff08;伪SPI协议&#xff09; 1.1 读写时序默认电平均为SCLK线低电平&#xff0c;CE线低电平 1.2 写数据&#xff0c;CE线拉高为高电平&#xff0c;开始传输数据&#xff0c;然后准备数…

2024年5月青岛教师编招聘报名详细流程

2024年5月青岛教师编招聘报名详细流程

【开发记录】青龙面板设置飞书机器人

接上篇文章&#xff0c;笔者在写上篇文章时对青龙面板的消息通知功能感兴趣&#xff0c;遂实验之&#xff0c;于是有了这篇文章。 首先参考这篇文章在群聊中引入一个机器人&#xff0c;此时可以获得该机器人的webhook。在青龙面板的通知设置中有larkKey一项&#xff0c;填入web…

[数据结构]————排序总结——插入排序(直接排序和希尔排序)—选择排序(选择排序和堆排序)-交换排序(冒泡排序和快速排序)—归并排序(归并排序)

文章涉及具体代码gitee&#xff1a; 登录 - Gitee.com 目录 1.插入排序 1.直接插入排序 总结 2.希尔排序 总结 2.选择排序 1.选择排序 ​编辑 总结 2.堆排序 总结 3.交换排序 1.冒泡排序 总结 2.快速排序 总结 4.归并排序 总结 5.总的分析总结 1.插入排…

用队列实现栈——leetcode刷题

题目的要求是用两个队列实现栈&#xff0c;首先我们要考虑队列的特点&#xff1a;先入先出&#xff0c;栈的特点&#xff1a;后入先出&#xff0c;所以我们的目标就是如何让先入栈的成员后出栈&#xff0c;后入栈的成员先出栈。 因为有两个队列&#xff0c;于是我们可以这样想&…

[Java EE] 多线程(七): 锁策略

&#x1f338;个人主页:https://blog.csdn.net/2301_80050796?spm1000.2115.3001.5343 &#x1f3f5;️热门专栏:&#x1f355; Collection与数据结构 (90平均质量分)https://blog.csdn.net/2301_80050796/category_12621348.html?spm1001.2014.3001.5482 &#x1f9c0;Java …

ZOC8 for Mac v8.08.1激活版:卓越性能的SSH客户端

在远程连接和管理的世界中&#xff0c;ZOC8 for Mac以其卓越的性能和丰富的功能&#xff0c;成为了众多专业人士的首选SSH客户端。它支持SSH1、SSH2、Telnet、Rlogin、Serial等多种协议&#xff0c;让您轻松连接到远程服务器。ZOC8拥有简洁直观的界面和强大的功能设置&#xff…

VMware虚拟机中ubuntu使用记录(6)—— 如何标定单目相机的内参(张正友标定法)

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、张正友相机标定法1. 工具的准备2. 标定的步骤(1) 启动相机(2) 启动标定程序(3) 标定过程的操作(5)可能的报错 3. 标定文件内容解析 前言 张正友相机标定法…