rust - 如何在 Rust 编译的二进制文件中包含文件内容?
问题描述
const fn get_dockerfile() -> String {
let mut file_content = String::new();
let mut file = File::open("dockers/PostgreSql").expect("Failed to read the file");
file.read_to_string(&mut file_content);
file_content
}
const DOCKERFILE: String = get_dockerfile();
我正在编写一个 Rust 脚本来管理 docker 操作。
- 我想在我的二进制可执行文件中包含 docker-file 内容。
- 我想通过将该内容分配给一个
const
变量,我可以实现这一点,但我收到了这个错误:
error[E0723]: mutable references in const fn are unstable
--> src/main.rs:9:5
|
9 | file.read_to_string(&mut file_content);
解决方案
使用include_str!
宏在编译时包含文件中的字符串。
const DOCKERFILE: &str = include_str!("dockers/PostgreSql");
推荐阅读
- ruby-on-rails - 重构!如何重用数据库查询中的变量来检索 Rails 控制器中的数据
- python - 如何在具有大量链接的 CSV 文件列中查找 img 标签 url,并将该链接与其他 CSV 文件中的相同链接进行比较
- java - 在 IntelliJ Idea 2020.1.4 中评估 java 流表达式时出现编译错误
- python - 用于保护未初始化数据库的 Twisted Deferred 模式
- mysql - 如何将 Postgresql 布尔值转换为 MySQL Tinyint?
- python - Python 中的默认模块
- sql - 关键字“select”附近的语法不正确。购买价值最高的员工详细信息
- uicollectionview - 图像未正确设置到 collectionview 单元格
- sql - 创建 SQL 查询以使用动态 where 子句/搜索条件在多个单向一对多表中进行搜索
- moryx - 如何为 ModelSetup 定义上下文