学习Rust的第27天:Rust中的pwd

过去几天我们一直在重新创建 GNU 核心实用程序的基本功能,而今天,我们将做一些有点太简单的事情, pwd 这个实用程序是用于打印Linux终端中的工作目录。

Understanding the utility
了解实用程序

Running the pwd command, we get an output like this
运行 pwd 命令,我们得到如下输出

# pwd
/root/

Here /root is the directory that we are currently in, in the Linux Shell
这里 /root 是我们当前所在的目录,在 Linux Shell 中

To recreate this, it’s really not difficult and we can recreate this without any trouble using the std::env::current_dir() function, Let’s create a library crate inside a new Rust project and a module to get started with this.
要重新创建它,实际上并不困难,我们可以使用 std::env::current_dir() 函数毫无困难地重新创建它,让我们在新的 Rust 项目中创建一个库箱和一个模块来开始使用它。

//lib.rs

pub mod dir_get {
  use std::env;
  use std::path::PathBuf;

  fn get_working_directory() -> std::io::Resullt<PathBuf> {
    env::current_dir()
  }
}

The get_working_directoryfunction runs the env::current_dir() function and returns a result enum with a PathBuf or an Err variant.
get_working_directory 函数运行 env::current_dir() 函数并返回带有 PathBuf 或 Err 变体的结果枚举。

We will do the error handing in the next part…
我们将在下一部分中进行错误处理......

  • 39
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老父亲的能量嘎嘣脆

感谢支持,共同成长

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值