我需要在我的Rust二进制文件中包含一个json
和一个toml
文件。这是一个独立的应用程序,人们不想在运行时传递配置文件。
include_str!
可以满足我的需求。我可以写:
static SETTINGS_FILE_STR: &str = include_str!(r"../my_config/settings.toml");
有没有比 r"../my_config/settings.toml"
更好的写文件路径的方法?
我似乎无法从 use std::path::{Path, PathBuf};
或 env
中任何内容构造字符串字面值。我想知道是否可以从 cargo.toml
文件中读取一些内容。但是没有成功。
我总是遇到:
error: argument must be a string literal
--> src/main.rs:23:42
|
23 | static SETTINGS_STR: &str = include_str!(FANCY_PATH_TO_TOML_FILE);
| ^^^^^^^^^^^^
我无法执行以下操作,因为
String
不是字符串字面量:fn get_config_path() -> String {
let root_dir = project_root::get_project_root().with_context(|| format!("Failed to get project root directory"))?;
const path: PathBuf = root_dir.join("my_config/settings.toml");
path.to_string()
}
如果这是 C/Objective-C,我可以使用构造函数或类函数来实现我想要的功能。正如你所猜测的那样,我对 Rust 还不熟悉。