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

在本文中复刻了 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.mfbz.cn/a/591671.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

C#实战—代码实现收发文件智能化

在信息化的今天&#xff0c;收发电子文档几乎是每个朋友都要经历的事情。比如班级学委和班长需要收发作业&#xff0c;企业管理者需要收发工作文件。但是&#xff01;&#xff01;&#xff01; 每到要交结果时&#xff0c;往往会发现总会有一些人没有即使交上&#xff0c;50个…

基于Springboot的校园食堂订餐系统(有报告)。Javaee项目,springboot项目。

演示视频&#xff1a; 基于Springboot的校园食堂订餐系统&#xff08;有报告&#xff09;。Javaee项目&#xff0c;springboot项目。 项目介绍&#xff1a; 采用M&#xff08;model&#xff09;V&#xff08;view&#xff09;C&#xff08;controller&#xff09;三层体系结构…

区域文本提示的实时文本到图像生成;通过一致性自注意力机制的视频生成工具保持视频的一致性;专门为雪佛兰汽车设计的客服聊天机器人

✨ 1: StreamMultiDiffusion StreamMultiDiffusion是首个基于区域文本提示的实时文本到图像生成框架&#xff0c;实现了高速且互动的图像生成。 StreamMultiDiffusion 旨在结合加速推理技术和基于区域的文本提示控制&#xff0c;以克服之前解决方案中存在的速度慢和用户交互性…

从零开始学AI绘画,万字Stable Diffusion终极教程(一)

【第1期】SD入门 2022年8月&#xff0c;一款叫Stable Diffusion的AI绘画软件开源发布&#xff0c;从此开启了AIGC在图像上的爆火发展时期 率先学会SD的人&#xff0c;已经挖掘出了越来越多AI绘画有趣的玩法 从开始的AI美女、线稿上色、真人漫改、头像壁纸 到后来的AI创意字、AI…

望仙谷听谿涛

望仙谿涛 近来不知为何&#xff0c;染上喝咖啡的恶习&#xff0c;称为“恶”&#xff0c;是因为要花钱&#xff0c;而且非得是那种口感好的。 网络流行“人生无解&#xff0c;来杯拿铁”。 大抵是因为咖啡再苦&#xff0c;也比不过生活吧&#xff0c;至少咖啡可以加糖&#xff…

机器学习批量服务模式优化指南

原文地址&#xff1a;optimizing-machine-learning-a-practitioners-guide-to-effective-batch-serving-patterns 2024 年 4 月 15 日 简介 在机器学习和数据分析中&#xff0c;模型服务模式的战略实施对于在生产环境中部署和操作人工智能模型起着至关重要的作用。其中&…

STM32——WWDG(窗口看门狗)

技术笔记&#xff01; 1.WWDG&#xff08;窗口看门狗&#xff09;简介 本质&#xff1a;能产生系统复位信号和提前唤醒中断的计数器。 特性&#xff1a; 递减的计数器&#xff1b; 当递减计数器值从 0x40减到0x3F时复位&#xff08;即T6位跳变到0&#xff09;&#xff1b; …

HTML_CSS学习:CSS盒子模型

一、CSS中常用的长度单位 相关代码&#xff1a; <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>CSS中常用的长度单位</title><style>html{font-size: 40px;}#d1{/*第一种长度单位&…

springboot+vue中小学文具商城购物系统网站

技术栈 前端&#xff1a;vue.jsElementUI 开发工具&#xff1a;IDEA 或者eclipse都支持 编程语言: java 框架&#xff1a; ssm/springboot 数据库: mysql 版本不限 数据库工具&#xff1a;Navicat/SQLyog都可以 详细技术&#xff1a;javaspringbootvueMYSQLMAVEN文具网站为用户…

【基于MAX98357的Minimax(百度)长文本语音合成TTS 接入教程】

【基于MAX98357的Minimax&#xff08;百度&#xff09;长文本语音合成TTS 接入教程】 1. 前言2. 先决条件2.1 硬件准备2.2 软件准备2.3 接线 3. 核心代码3.1 驱动实现3.2 代码解析 4. 播放文本5. 结论 视频地址&#xff1a; SeeedXIAO ESP32S3 Sense【基于MAX98357的Minimax&am…

8.MyBatis 操作数据库(进阶)

文章目录 1.动态SQL插入1.1使用注解方式插入数据1.2使用xml方式插入数据1.3何时用注解何时用xml&#xff1f;1.4使用SQL查询中有多个and时&#xff0c;如何自动去除多余and1.4.1方法一&#xff1a;删除and之后的代码如图所示&#xff0c;再次运行1.4.2方法二&#xff1a;加上tr…

MATLAB实现遗传算法优化同时取送货的车辆路径问题VRPSDP

同时取送货的车辆路径问题VRPSDP的数学模型如下: 模型假设 所有车辆的载重、容量等性能相同。每个客户的需求&#xff08;送货和取货量&#xff09;是已知的&#xff0c;且在服务过程中不会改变。车辆的行驶速度恒定&#xff0c;不考虑交通拥堵等实时路况变化。每个客户点只能…

【C语言】——结构体

【C语言】——结构体 一、结构体类型的声明1.1、结构体的声明1.2、结构体变量的创建和初始化1.3、结构体的特殊声明1.4、结构体的自引用1.5、结构体的重命名 二、 结构体的内存对齐2.1、对齐规则2.2、结构体对齐实践2.3、为什么存在内存对齐2.4、修改默认对齐数 三、结构体传参…

数据结构------栈的介绍和实现

目录 1.栈的一些初步认识 2.栈的实现 3.相关的函数介绍 &#xff08;1&#xff09;栈的初始化 &#xff08;2&#xff09;栈的销毁 &#xff08;3&#xff09;栈的数据插入 &#xff08;6&#xff09;判断是否为空 &#xff08;7&#xff09;栈的大小 4.栈的实现完整…

C语言例题31:在屏幕上显示一个菱形

题目要求&#xff1a;在屏幕上显示一个菱形 #include <stdio.h>void main() {int i, j;int x;printf("输入菱形行数(3以上的奇数&#xff09;&#xff1a;");scanf("%d", &x);//显示菱形上面的大三角形for (i 1; i < (x 1) / 2; i) {for (…

【R语言数据分析】相关性分析:pearson与spearman

相关性分析是探寻两个变量之间关联关系的分析方法&#xff0c;注意相关性分析仅仅针对连续型变量和有序分类变量&#xff0c;对于无需分类变量就不存在相关性分析了&#xff0c;而是通过差异分析来间接反映相关性。比如性别和身高的关系就无法做相关性分析&#xff0c;虽然我们…

RHCE shell-第一次作业

要求&#xff1a; 1、判断当前磁盘剩余空间是否有20G&#xff0c;如果小于20G&#xff0c;则将报警邮件发送给管理员&#xff0c;每天检査- 次磁盘剩余空间。 2、判断web服务是否运行(1、查看进程的方式判断该程序是否运行&#xff0c;2、通过查看端口的方式 判断该程序是否运…

动态规划——最短编辑距离

一、问题描述 最短编辑距离(Minimum Edit Distance)&#xff0c;也被称为Levenshtein距离&#xff0c;是一种计算两个字符串间的差异程度的字符串度量(string metric)。我们可以认为Levenshtein距离就是从一个字符串修改到另一个字符串时&#xff0c;其中编辑单个字符&#xff…

从零开始学AI绘画,万字Stable Diffusion终极教程(二)

【第2期】关键词 欢迎来到SD的终极教程&#xff0c;这是我们的第二节课 这套课程分为六节课&#xff0c;会系统性的介绍sd的全部功能&#xff0c;让你打下坚实牢靠的基础 1.SD入门 2.关键词 3.Lora模型 4.图生图 5.controlnet 6.知识补充 在第一节课里面&#xff0c;我们…

CPP#类与对象4

友元 关键字&#xff1a;friend 友元的实现&#xff1a;全局函数做友元&#xff1b; 类做友元&#xff1b; 成员函数做友元。 .1全局函数做友元 class Point { private:double x, y; public:Point(double xx, double yy); friend int Distance(Point &a, Point &b)…
最新文章