如何以尽可能少的依赖项获取当前工作目录?

发布于 2025-01-10 05:05:06 字数 352 浏览 3 评论 0原文

我有一个 Rust 应用程序,它作为 shell 工作。我想使用尽可能少的依赖项来获取用户所在的工作目录。

我尝试使用

let path = env::current_dir();
print!("{} > ",path.display());

但它给了我以下错误: method not found in `Result'

我尝试在 Rust 游乐场中运行它,它工作正常,所以我想知道你们谁能帮我解决这个问题

I have a Rust application which works as a shell. I want to get the working directory the user is in using as little dependencies as possible.

I tried using

let path = env::current_dir();
print!("{} > ",path.display());

But it gives me the following error: method not found in `Result<PathBuf, std::io::Error>'

I tried running this in the rust playground and it worked alright, so I was wondering if any of you could help me with this

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

身边 2025-01-17 05:05:06

您可以使用 std::env::current_dir 获取当前工作目录。

您还需要使用 match, std::result::Result::unwrapstd::result::Result::expect, ?,或其他内容处理错误 Result 的变体并访问里面的值好的变体。

You can use std::env::current_dir to get the current working directory.

You also need to use match, std::result::Result::unwrap, std::result::Result::expect, ?, or something else to handle the Err variant of the Result and access the value inside the Ok variant.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文