修改内容

修改理由:
相关信息:
- 类型:文档文章
- 文章: 不稳定的功能
- 文档: 《Cargo 教程(2018)》
此投稿已在 6年前 合并。
内容修改:
Old | New | Differences |
---|---|---|
1 | ## Unstable Features | |
1 | ## Unstable Features | |
2 | ||
3 | > 不稳定的特性 | |
4 | ||
5 | 实验性 Cargo 特性仅适用于夜间通道(Rust 的 nightly 版本)。您通常使用`-Z`带其中一个特性,以启用它们。运行`cargo -Z help`查看可用的标志列表。 | |
6 | ||
7 | `-Z unstable-options`是用于启用其他不稳定命令行标志的通用标志。需要的此选项将在下面列出。 | |
8 | ||
9 | 某些不稳定的特性需要您,在`Cargo.toml`指定`cargo-features`字段。 | |
10 | ||
11 | ### Alternate Registries | |
12 | ||
13 | > 替换(/备用)注册表 | |
14 | ||
15 | - RFC:[#2141](https://github.com/rust-lang/rfcs/blob/master/text/2141-alternative-registries.md) | |
16 | - 跟踪问题:[rust-lang/rust#44931](https://github.com/rust-lang/rust/issues/44931) | |
17 | ||
18 | 备用注册表,允许您使用 crates.io 以外的注册表。 | |
19 | ||
20 | 注册表的名称定义在`.cargo/config`中,`registries`表格的下面: | |
21 | ||
22 | ```toml | |
23 | [registries] | |
24 | my-registry = { index = "https://my-intranet:8080/git/index" } | |
25 | ``` | |
26 | ||
27 | 可以在`.cargo/credentials`添加备用注册表的身份验证信息: | |
28 | ||
29 | ```toml | |
30 | [registries.my-registry] | |
31 | token = "api-token" | |
32 | ``` | |
33 | ||
34 | 在`Cargo.toml`里面,您可以使用指定`registry`字段,让某个依赖项来自哪个注册表。但首先,您需要在文件的顶部包含适当的`cargo-features`: | |
35 | ||
36 | ```toml | |
37 | cargo-features = ["alternative-registries"] | |
38 | ||
39 | [package] | |
40 | ... | |
41 | ||
42 | [dependencies] | |
43 | other-create = { version = "1.0", registry = "my-registry"} | |
44 | ``` | |
45 | ||
46 | 一个`--registry`标志 已添加到与注册表交互的`publish`,`login`等命令中。示例: | |
47 | ||
48 | ``` | |
49 | cargo +nightly publish -Z unstable-options --registry my-registry | |
50 | ``` | |
51 | ||
52 | 在`Cargo.toml`的 `publish` 字段,已被扩展为接受限制为发布到这些注册表的注册表列表。 | |
53 | ||
54 | ```toml | |
55 | [package] | |
56 | ... | |
57 | publish = ["my-registry"] | |
58 | ``` | |
59 | ||
60 | ### publish-lockfile | |
61 | ||
62 | > 发布-锁文件 | |
63 | ||
64 | - 原始问题:[#2263](https://github.com/rust-lang/cargo/issues/2263) | |
65 | - PR:[#5093](https://github.com/rust-lang/cargo/pull/5093) | |
66 | - 跟踪问题:[#5654](https://github.com/rust-lang/cargo/issues/5654) | |
67 | ||
68 | 创建`.crate`文件分发时,Cargo 历史上不包括`Cargo.lock`文件。这可能会导致`cargo install`用于二进制文件的问题。您可以在`cargo package`要么`cargo publish`使用时,指定您的包应包含`Cargo.lock`,方法是在`Cargo.toml`中指定`publish-lockfile`字段。这也需要适当的`cargo-features`要求: | |
69 | ||
70 | ```toml | |
71 | cargo-features = ["publish-lockfile"] | |
72 | ||
73 | [package] | |
74 | ... | |
75 | publish-lockfile = true | |
76 | ``` | |
77 | ||
78 | ### Offline Mode | |
79 | ||
80 | > 离线模式 | |
81 | ||
82 | - 原始问题:[#4686](https://github.com/rust-lang/cargo/issues/4686) | |
83 | - 跟踪问题:[#5655](https://github.com/rust-lang/cargo/issues/5655) | |
84 | ||
85 | `-Z offline`标志 可防止 Cargo 因任何原因尝试访问网络。通常情况下,如果 Cargo 要访问网络但它不可用,则会因错误而停止。 | |
86 | ||
87 | 请注意,这可能会导致与在线模式不同的依赖项解析。即使可能有索引的较新版本的本地副本,Cargo 也会将自己限制在本地可用的箱(crate)中。 | |
88 | ||
89 | ### no-index-update | |
90 | ||
91 | > 无索引更新 | |
92 | ||
93 | - 原始问题:[#3479](https://github.com/rust-lang/cargo/issues/3479) | |
94 | ||
95 | `-Z no-index-update`标志 确保 Cargo 不会尝试更新注册表索引。这适用于测试(调制)许多 Cargo 命令的 Crater 等工具,并且您希望每次都避免更新索引的网络延迟。 | |
96 | ||
97 | ### avoid-dev-deps | |
98 | ||
99 | > 阻止 开发依赖项 | |
100 | ||
101 | - 原始问题:[#4988](https://github.com/rust-lang/cargo/issues/4988) | |
102 | - 稳定问题:[#5133](https://github.com/rust-lang/cargo/issues/5133) | |
103 | ||
104 | 运行命令如`cargo install`要么`cargo build`时,Cargo 当前会需要下载 dev 依赖项,即使它们未被使用。而该`-Z avoid-dev-deps`标志 则让 Cargo 避免在不需要时下载 dev 依赖项。该`Cargo.lock`如果跳过了 dev-dependencies,将不会生成相关信息。 | |
105 | ||
106 | ### minimal-versions | |
107 | ||
108 | > 最小的版本 | |
109 | ||
110 | - 原始问题:[#4100](https://github.com/rust-lang/cargo/issues/4100) | |
111 | - 跟踪问题:[#5657](https://github.com/rust-lang/cargo/issues/5657) | |
112 | ||
113 | 当一个`Cargo.lock`文件被生成,`-Z minimal-versions`标志的使用, 将解析依赖关系为满足要求的最小 semver 版本(而不是最高版本)。 | |
114 | ||
115 | 此标志的预期用例,是在持续集成期间,检查 Cargo.toml 中指定的版本是否是您实际使用的最低版本的正确反映。也就是说,如果 Cargo.toml 有`foo = "1.0.0"`说明,那您依赖该特性,不会意外地添加`foo 1.5.0`。 | |
116 | ||
117 | ### out-dir | |
118 | ||
119 | > 输出目录 | |
120 | ||
121 | - 原始问题:[#4875](https://github.com/rust-lang/cargo/issues/4875) | |
122 | ||
123 | 此功能允许您指定,构建工件后,将复制到的目录。通常,工件只写入`target/release`要么`target/debug`目录。但是,确定明确的文件名可能很棘手,因为您需要解析 JSON 输出。而该`--out-dir`标志 可以更容易地预测访问工件。但是请注意,工件只是被复制,因此原件仍在`target`目录.例: | |
124 | ||
125 | ``` | |
126 | cargo +nightly build --out-dir=out -Z unstable-options | |
127 | ``` | |
128 | ||
129 | ### Profile Overrides | |
130 | ||
131 | > 覆盖配置 | |
132 | ||
133 | - 跟踪问题:[rust-lang/rust#48683](https://github.com/rust-lang/rust/issues/48683) | |
134 | - RFC:[#2282](https://github.com/rust-lang/rfcs/blob/master/text/2282-profile-dependencies.md) | |
135 | ||
136 | 可以为特定包和自定义生成脚本覆盖配置文件。一般格式如下: | |
137 | ||
138 | ```toml | |
139 | cargo-features = ["profile-overrides"] | |
140 | ||
141 | [package] | |
142 | ... | |
143 | ||
144 | [profile.dev] | |
145 | opt-level = 0 | |
146 | debug = true | |
147 | ||
148 | # 这个 `image` 箱 会编译的等级 -Copt-level=3 | |
149 | [profile.dev.overrides.image] | |
150 | opt-level = 3 | |
151 | ||
152 | # 所有 依赖项 (但 不是 箱本身 或 某些工作区成员) | |
153 | # 会编译的等级 -Copt-level=2 。 其中包括 构建依赖项. | |
154 | [profile.dev.overrides."*"] | |
155 | opt-level = 2 | |
156 | ||
157 | # 构建 脚本和他们的 依赖项, 会编译的等级 -Copt-level=3 | |
158 | # 默认情况, 构建 脚本 对剩下的配置使用相同的选择 | |
159 | [profile.dev.build-override] | |
160 | opt-level = 3 | |
161 | ``` | |
162 | ||
163 | 能只为 dev 和 release 配置文件指定覆盖。 | |
164 | ||
165 | ### Config Profiles | |
166 | ||
167 | > 配置 配置文件 (第一个动词,第二个名词) | |
168 | ||
169 | - 跟踪问题:[rust-lang/rust#48683](https://github.com/rust-lang/rust/issues/48683) | |
170 | - RFC:[#2282](https://github.com/rust-lang/rfcs/blob/master/text/2282-profile-dependencies.md) | |
171 | ||
172 | <!-- HERE --> | |
173 | ||
174 | 可以在`.cargo/config`文件中指定配置文件。该`-Z config-profile`命令行标志是使用此功能所必需的。格式与一个 `Cargo.toml`清单格式相同。如果在多个文件中找到相同的配置,则通过使用常规设置[配置层次结构](https://learnku.com/docs/cargo-book/{{version}}/config/4775#hierarchical-structure)合并。配置设置优先于清单设置。 | |
175 | ||
176 | ```toml | |
177 | [profile.dev] | |
178 | opt-level = 3 | |
179 | ``` | |
180 | ||
181 | ``` | |
182 | cargo +nightly build -Z config-profile | |
183 | ``` | |
184 | ||
185 | ### Namespaced features | |
186 | ||
187 | > 特性的命名区间化 | |
188 | ||
189 | - 原始问题:[#1286](https://github.com/rust-lang/cargo/issues/1286) | |
190 | - 跟踪问题:[rust-lang/Cargo#5565](https://github.com/rust-lang/cargo/issues/5565) | |
191 | ||
192 | 目前,不可能在清单中具有相同名称的特性和依赖项。但如果你设置`namespaced-features`至`true`,功能和依赖项的命名空间就会是分开的。这样做的结果是,在特性请求中,依赖项必须以`crate:`为前缀。像这样: | |
193 | ||
194 | ```toml | |
195 | [package] | |
196 | namespaced-features = true | |
197 | ||
198 | [features] | |
199 | bar = ["crate:baz", "foo"] | |
200 | foo = [] | |
201 | ||
202 | [dependencies] | |
203 | baz = { version = "0.1", optional = true } | |
204 | ``` | |
205 | ||
206 | 为了防止不必要的,必须为每个可选依赖项显式声明特性的模版,将为任何不是定义为相同名称的特性,的可选依赖项创建隐式特性。但是,如果定义了与依赖项同名的特性,则该特性必须将依赖项作为必需项,正如`foo = ["crate:foo"]`。 | |
207 | ||
208 | ### Build-plan | |
209 | ||
210 | > 构建计划 | |
211 | ||
212 | - 跟踪问题:[rust-lang/Cargo#5579](https://github.com/rust-lang/cargo/issues/5579) | |
213 | ||
214 | `build`命令的`--build-plan`参数,将输出 JSON,其中包含有关将运行哪些命令,和不实际执行任何操作的信息。与其他构建工具集成时,这可能很有用。例: | |
215 | ||
216 | ``` | |
217 | cargo +nightly build --build-plan -Z unstable-options | |
218 | ``` | |
219 | ||
220 | ### default-run | |
221 | ||
222 | > 默认运行 | |
223 | ||
224 | - 原始问题:[#2200](https://github.com/rust-lang/cargo/issues/2200) | |
225 | ||
226 | 清单中`[package]`部分的该`default-run`选项,可用于指定`cargo run`选择的默认二进制文件。例如,当存在`src/bin/a.rs`和`src/bin/b.rs`两者时,选择前者: | |
227 | ||
228 | ```toml | |
229 | [package] | |
230 | default-run = "a" | |
231 | ``` | |
232 | ||
233 | ### Metabuild | |
234 | ||
235 | > 元构建 | |
236 | ||
237 | - 跟踪问题:[rust-lang/rust#49803](https://github.com/rust-lang/rust/issues/49803) | |
238 | - RFC:[#2196](https://github.com/rust-lang/rfcs/blob/master/text/2196-metabuild.md) | |
239 | ||
240 | Metabuild 是一个具有声明性构建脚本的特性。作为不去写一个`build.rs`脚本,而是您在`Cargo.toml`中的`metabuild`字段,指定构建依赖项列表。其将自动生成一个构建脚本,该脚本按顺序运行每个构建依赖项。然后,Metabuild 包可以从`Cargo.toml`中读取元数据,执行他们的指定行为。 | |
241 | ||
242 | 需要包括`cargo-features`在`Cargo.toml`的顶部, 一个`metadata`字段在`package`下,列出`build-dependencies`依赖项,并添加 metabuild 包所需的任何元数据。例: | |
243 | ||
244 | ```toml | |
245 | cargo-features = ["metabuild"] | |
246 | ||
247 | [package] | |
248 | name = "mypackage" | |
249 | version = "0.0.1" | |
250 | metabuild = ["foo", "bar"] | |
251 | ||
252 | [build-dependencies] | |
253 | foo = "1.0" | |
254 | bar = "1.0" | |
255 | ||
256 | [package.metadata.foo] | |
257 | extra-info = "qwerty" | |
258 | ``` | |
259 | ||
260 | Metabuild 包应该有一个名`metabuild`为的公共函数,它会执行与常规`build.rs`脚本一样,执行相同操作。 | |
2 | 261 | |
3 | ||
4 | ||
5 | ||
6 | ||
7 | ||
8 | ||
9 | ||
10 | ||
11 | ||
12 | ||
13 | ||
14 | ||
15 | ||
16 | ||
17 | ||
18 | ||
19 | ||
20 | ||
21 | ||
22 | ||
23 | ||
24 | ||
25 | ||
26 | ||
27 | ||
28 | ||
29 | ||
30 | ||
31 | ||
32 | ||
33 | ||
34 | ||
35 | ||
36 | ||
37 | ||
38 | ||
39 | ||
40 | ||
41 | ||
42 | ||
43 | ||
44 | ||
45 | ||
46 | ||
47 | ||
48 | ||
49 | ||
50 | ||
51 | ||
52 | ||
53 | ||
54 | ||
55 | ||
56 | ||
57 | ||
58 | ||
59 | ||
60 | ||
61 | ||
62 | ||
63 | ||
64 | ||
65 | ||
66 | ||
67 | ||
68 | ||
69 | ||
70 | ||
71 | ||
72 | ||
73 | ||
74 | ||
75 | ||
76 | ||
77 | ||
78 | ||
79 | ||
80 | ||
81 | ||
82 | ||
83 | ||
84 | ||
85 | ||
86 | ||
87 | ||
88 | ||
89 | ||
90 | ||
91 | ||
92 | ||
93 | ||
94 | ||
95 | ||
96 | ||
97 | ||
98 | ||
99 | ||
100 | ||
101 | ||
102 | ||
103 | ||
104 | ||
105 | ||
106 | ||
107 | ||
108 | ||
109 | ||
110 | ||
111 | ||
112 | ||
113 | ||
114 | ||
115 | ||
116 | ||
117 | ||
118 | ||
119 | ||
120 | ||
121 | ||
122 | ||
123 | ||
124 | ||
125 | ||
126 | ||
127 | ||
128 | ||
129 | ||
130 | ||
131 | ||
132 | ||
133 | ||
134 | ||
135 | ||
136 | ||
137 | ||
138 | ||
139 | ||
140 | ||
141 | ||
142 | ||
143 | ||
144 | ||
145 | ||
146 | ||
147 | ||
148 | ||
149 | ||
150 | ||
151 | ||
152 | ||
153 | ||
154 | ||
155 | ||
156 | ||
157 | ||
158 | ||
159 | ||
160 | ||
161 | ||
162 | ||
163 | ||
164 | ||
165 | ||
166 | ||
167 | ||
168 | ||
169 | ||
170 | ||
171 | ||
172 | ||
173 | ||
174 | ||
175 | ||
176 | ||
177 | ||
178 | ||
179 | ||
180 | ||
181 | ||
182 | ||
183 | ||
184 | ||
185 | ||
186 | ||
187 | ||
188 | ||
189 | ||
190 | ||
191 | ||
192 | ||
193 | ||
194 | ||
195 | ||
196 | ||
197 | ||
198 | ||
199 | ||
200 | ||
201 | ||
202 | ||
203 | ||
204 | ||
205 | ||
206 | ||
207 | ||
208 | ||
209 | ||
210 | ||
211 | ||
212 | ||
213 | ||
214 | ||
215 | ||
216 | ||
217 | ||
218 | ||
219 | ||
220 | ||
221 | ||
222 | ||
223 | ||
224 | ||
225 | ||
226 | ||
227 | ||
228 | ||
229 | ||
230 | ||
231 | ||
232 | ||
233 | ||
234 | ||
235 | ||
236 | ||
237 | ||
238 | ||
239 | ||
240 | ||
241 | ||
242 | ||
243 | ||
244 | ||
245 | ||
246 | ||
247 | ||
248 | ||
249 | ||
250 | ||
251 | ||
252 | ||
253 | ||
254 | ||
255 | ||
256 | ||
257 | ||
258 | ||
259 | ||
260 | ||
261 |