如何避免 Rust 中的硬编码值 [英] How to avoid hard-coded values in Rust

查看:56
本文介绍了如何避免 Rust 中的硬编码值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是 Maven/Java 目录结构.

Below is a Maven/Java directory structure.

- src
  - main
    - java
    - resources
  - test
    - java
    - resources

- target

此处,resources 文件夹包含与应用程序相关的配置文件和资源文件,以避免将其内容硬编码到源文件中.

Here, the resources folder holds application-related configuration files and resource files, to avoid hard-coding their contents in source files.

如何使用 Cargo 在 Rust 中实现相同的效果?

How can I achieve the same in Rust with Cargo?

推荐答案

Maven 并不包括您的源代码中的所有内容.事实上,它包含二进制文件,但根本不需要在 .jar 中包含源代码.您可以将其配置为说明要包含的内容,默认情况下是 resources 目录中的所有内容.

Maven doesn't include everything from your source. In fact, it includes the binary but doesn't even have to include the source at all in a .jar. You can configure it to say what to include, which by default is everything in the resources directory.

货物包源代码.所有内容 都将包含在 crate 中,但与您的 .gitignore 文件匹配的内容除外.您可以通过添加 includeexclude 条目在 [package] 部分.

Cargo packages source code. Everything will be included in the crate, with the exception of things that match your .gitignore file. You can fine-tune that in your Cargo.toml file, by adding an include or exclude entry in the [package] section.

要访问这些文件,有几个选项.

To access these files, there are a few options.

例如,如果您的项目如下所示:

For example, if your project looks like this:

- Cargo.toml
- src
   - main.rs
- resources
   - hello.txt

访问hello.txt主要有以下三种方式.

There are three main ways to access hello.txt, as follows.

使用 include! 宏,您可以像这样从 main.rs 访问 hello.txt:

With the include! macro, you can access hello.txt from main.rs like this:

let hello: &str = include!("../resources/hello.txt");

请注意,宏将直接在源文件中包含文件,就像您复制 & 一样.粘贴文件的内容.所以我在那里给出的例子只有在文件的内容包含 "" 引号时才有效.任何 Rust 源都可以放在那里,并且必须在编译时包含在内.这可以方便地包含复杂的 Rust 结构,而无需编写解析代码.请注意,该路径相对于包含它的源 .rs 文件.

Be aware that the macro will include the file directly in the source, exactly as if you copy & pasted the contents of the file. So the example I gave there would work only if the contents of the file include "" quotes. Any Rust source can go there, and it has to be included at compile-time. This can be convenient for including complicated Rust structs, without having to write parsing code. Notice that the path is relative to the source .rs file that included it.

include_bytes! 宏从文件中创建一个固定大小的 u8 数组,以在编译时包含.

The include_bytes! macro creates a fixed-size array of u8 from the file, to be included at compile-time.

let bytes = include_bytes!("../resources/hello.txt").
let hello: String = String::from_bytes_lossy(bytes).to_string();

这便于将任意二进制数据合并到您的应用程序中,例如图像,而无需在运行时加载它.该数组具有 'static 生命周期,因此将在应用程序的整个生命周期中保留在内存中.

This is convenient for incorporating arbitrary binary data into your application, for example an image, without having to load it at runtime. The array has 'static lifetime, so will stay in memory for the entire lifetime of your application.

include_str! 宏的工作原理类似,但会产生一个字符串切片,也具有 'static 生命周期.

The include_str! macro works similarly, but will result in a string slice, also with 'static lifetime.

要在运行时加载文件,您可以使用:

To load a file at run-time, you can use:

let hello = std::fs::read_to_string("resources/hello.txt").unwrap();

此处的路径相对于您的 crate 的根目录.这假设您正在使用 cargo run 从构建它的位置运行应用程序.如果您要部署二进制文件,则可能必须提供应用程序应在何处找到其资源的路径.

The path here is relative to the root of your crate. This assuming that you are running the application with cargo run from where it was built. If you are deploying a binary, you may have to supply a path to where your application should find its resources.

通常,这是首选方法.它更灵活,因为您可以在运行时交换配置或使用应用程序参数从不同位置加载.有很多用于解析不同文件格式的 crate,包括 jsontoml 和很多其他的.您可以控制所加载数据的生命周期,因此您可以确保在完成后将其释放.

Usually, this is the preferred method. It's more flexible as you can swap the config at runtime or use application arguments to load from a different location. There are lots of crates for parsing different file formats, including json, toml and plenty of others. You have control over the lifetime of the data you load, so you can make sure it's deallocated when you are finished with it.

这篇关于如何避免 Rust 中的硬编码值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆