如何以尽可能少的依赖项获取当前工作目录?
我有一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
std::env::current_dir
获取当前工作目录。您还需要使用
match
,std::result::Result::unwrap
,std::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 theErr
variant of theResult
and access the value inside theOk
variant.