如何访问通过 `cargo install` 安装的 Rust/Cargo 项目中包含的资产? [英] How do I access assets included in a Rust/Cargo project installed via `cargo install`?
问题描述
我有一个项目,其中包含一些我需要在运行时找到的相关资产(Lua 脚本).这可能意味着两件事:
I have a project which includes some associated assets (Lua scripts), which I need to find at runtime. This can mean two things:
- 在开发过程中(例如
cargo run
),我想找到相对于源代码 - 当通过
cargo install
安装时,资产也应该安装在某个地方,并且可执行文件的安装版本应该找到已安装的资产.
- During development (e.g.
cargo run
), I want to find it relative to the source - When installed via
cargo install
, the assets should be installed somewhere as well, and the installed version of the executable should find the installed assets.
我知道可以使用诸如 include_str!()
之类的选项将文本文件编译为二进制文件,但在这种情况下我不想这样做.
I know about the option to use something like include_str!()
to compile text files into the binary, but I don't want to do that in this case.
据我所知,cargo install
doesn'目前不支持安装可执行文件以外的任何东西,这是第一个问题,尽管我不介意有一个包装器安装脚本来帮助.
As far as I can tell, cargo install
doesn't have any support for installing anything other than the executable at the moment, which is the first problem, though I don't mind having a wrapper install script to help.
推荐答案
根据你想要的结构,你可以尝试混合使用 env::current_dir
获取当前目录,如果没有找到任何内容,您可以尝试使用可执行文件路径并从那里加载东西.你可以通过 env::current_exe
.
Depending on how you want it structured, you could try a mix of env::current_dir
to get the current directory, and if that doesn't find anything you can try using the executable path and load things from there. You get that with env::current_exe
.
我看到了困难,那就是 cargo install
only 将二进制文件复制到 .cargo/bin
中,这意味着额外的资源留在位于 .cargo/registry/src/...
的源文件夹.
I see the difficulty, which is that cargo install
only copies the binary into .cargo/bin
which means that additional resources stay in the source folder located at .cargo/registry/src/...
.
在这种情况下,我同意@ljedrzinclude!
似乎是唯一的方法.
In this case, I agree with @ljedrz that include!
seems to be the only way.
这篇关于如何访问通过 `cargo install` 安装的 Rust/Cargo 项目中包含的资产?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!