阐明生锈文件路径和访问

发布于 2025-02-03 06:23:50 字数 366 浏览 4 评论 0原文

Rust非常新,正在寻找有关项目目录之外的Rust文件路径的澄清。我希望从桌面上读取一个.txt文件看起来大致类似的东西:

fs::read_to_string("./Desktop/test.txt");

但是,这似乎不起作用。这是列出的文件路径的问题,还是RUST仅允许在项目目录中访问文件?如果这是默认情况,如何允许在当前工作目录之外的系统中其他地方访问文件? 假设该项目在文档中,我们想访问桌面上的一些文本文件。

找到答案,看来我正在寻找的道路就是这样:

fs::read_to_string("/Desktop/test.txt");

似乎是“”。正在查看当前目录。

Very new to Rust and am looking for clarification about rust file paths that are outside the project directory. I would expect reading a .txt file from say the desktop to look something roughly like this:

fs::read_to_string("./Desktop/test.txt");

However, this does not seem to work. Is this an issue with the file path listed or does Rust only allow access to files in the project directory? If this is the default case how does one allow access to files elsewhere in the system outside the current working directory?
Say the project is in Documents and we want to access some text file on the Desktop.

Found the answer, seems the path that I am looking for is like this :

fs::read_to_string("/Desktop/test.txt");

Seems the "." was looking in the current directory.

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

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

发布评论

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

评论(2

撩动你心 2025-02-10 06:23:50

当您打开以为./开头的路径时,它相对于当前工作目录

假设程序不更改工作目录 ,那将是用户启动可执行文件时的任何目录。

请注意,不一定是项目目录:开发程序时,您可能会通过货物运行运行它,但是当它准备就绪时,您很可能会将目标可执行文件复制到一个目录中在路径中。工作目录可能与放置可执行文件的目录完全不同。

您的程序可以通过调用 std :: env :: current_dir

如果您想要不取决于工作目录的路径,则可以使用绝对路径,即。从/开始。

在这方面,生锈与任何其他编程语言没有什么不同。特别是,关于项目dir的文件未打开。

When you open a path that starts with ./, it is relative to the current working directory.

Assuming that the program does not change the working directory itself, that would be whatever directory the user was in when they started the executable.

Note that it is not necessarily the project directory: when you are developing your program, you will probably run it via cargo run, but when it is ready you would most likely copy the target executable into a directory that is in the path. The working directory can be completely different to the directory in which the executable is placed.

Your program can find out the current working directory by calling std::env::current_dir

If you want a path that does not depend on the working directory, you can use an absolute path, ie. starting with /.

Rust is no different to any other programming language in this respect. In particular, files are not opened in respect to the project dir.

一枫情书 2025-02-10 06:23:50

如指定,该路径将相对于工作目录 。如果工作目录不是您的主目录,则需要更具体地了解文件所在的位置。

请注意,工作目录可以根据您运行程序的方式更改。上面的链接具有有关此主题的更多信息。

您可以使用dirs板条箱,这将使工作目录在这种情况下无关紧要。它还将正确确定许多不同操作系统上用户的桌面目录。

fs::read_to_string({
    let mut path = dirs::desktop_dir().expect("no desktop directory");
    path.push("test.txt");
    path
})

As specified, the path will be relative to the working directory of the process. If the working directory isn't your home directory, then you need to be more specific about where the file is.

Note that the working directory can change depending on how you run the program. The link above has more information on this topic.

You can obtain your user's desktop directory using desktop_dir from the dirs crate, which will make the working directory irrelevant in this particular case. It will also correctly determine the user's desktop directory on many different operating systems.

fs::read_to_string({
    let mut path = dirs::desktop_dir().expect("no desktop directory");
    path.push("test.txt");
    path
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文