1CARGO-RUSTC(1) General Commands Manual CARGO-RUSTC(1)
2
3
4
6 cargo-rustc - Compile the current package, and pass extra options to
7 the compiler
8
10 cargo rustc [options] [-- args]
11
13 The specified target for the current package (or package specified by
14 -p if provided) will be compiled along with all of its dependencies.
15 The specified args will all be passed to the final compiler invocation,
16 not any of the dependencies. Note that the compiler will still
17 unconditionally receive arguments such as -L, --extern, and
18 --crate-type, and the specified args will simply be added to the
19 compiler invocation.
20
21 See <https://doc.rust-lang.org/rustc/index.html> for documentation on
22 rustc flags.
23
24 This command requires that only one target is being compiled when
25 additional arguments are provided. If more than one target is available
26 for the current package the filters of --lib, --bin, etc, must be used
27 to select which target is compiled.
28
29 To pass flags to all compiler processes spawned by Cargo, use the
30 RUSTFLAGS environment variable
31 <https://doc.rust-lang.org/cargo/reference/environment-variables.html>
32 or the build.rustflags config value
33 <https://doc.rust-lang.org/cargo/reference/config.html>.
34
36 Package Selection
37 By default, the package in the current working directory is selected.
38 The -p flag can be used to choose a different package in a workspace.
39
40 -p spec, --package spec
41 The package to build. See cargo-pkgid(1) for the SPEC format.
42
43 Target Selection
44 When no target selection options are given, cargo rustc will build all
45 binary and library targets of the selected package.
46
47 Passing target selection flags will build only the specified targets.
48
49 Note that --bin, --example, --test and --bench flags also support
50 common Unix glob patterns like *, ? and []. However, to avoid your
51 shell accidentally expanding glob patterns before Cargo handles them,
52 you must use single quotes or double quotes around each glob pattern.
53
54 --lib
55 Build the package's library.
56
57 --bin name...
58 Build the specified binary. This flag may be specified multiple
59 times and supports common Unix glob patterns.
60
61 --bins
62 Build all binary targets.
63
64 --example name...
65 Build the specified example. This flag may be specified multiple
66 times and supports common Unix glob patterns.
67
68 --examples
69 Build all example targets.
70
71 --test name...
72 Build the specified integration test. This flag may be specified
73 multiple times and supports common Unix glob patterns.
74
75 --tests
76 Build all targets in test mode that have the test = true manifest
77 flag set. By default this includes the library and binaries built
78 as unittests, and integration tests. Be aware that this will also
79 build any required dependencies, so the lib target may be built
80 twice (once as a unittest, and once as a dependency for binaries,
81 integration tests, etc.). Targets may be enabled or disabled by
82 setting the test flag in the manifest settings for the target.
83
84 --bench name...
85 Build the specified benchmark. This flag may be specified multiple
86 times and supports common Unix glob patterns.
87
88 --benches
89 Build all targets in benchmark mode that have the bench = true
90 manifest flag set. By default this includes the library and
91 binaries built as benchmarks, and bench targets. Be aware that this
92 will also build any required dependencies, so the lib target may be
93 built twice (once as a benchmark, and once as a dependency for
94 binaries, benchmarks, etc.). Targets may be enabled or disabled by
95 setting the bench flag in the manifest settings for the target.
96
97 --all-targets
98 Build all targets. This is equivalent to specifying --lib --bins
99 --tests --benches --examples.
100
101 Feature Selection
102 The feature flags allow you to control which features are enabled. When
103 no feature options are given, the default feature is activated for
104 every selected package.
105
106 See the features documentation
107 <https://doc.rust-lang.org/cargo/reference/features.html#command-line-feature-options>
108 for more details.
109
110 -F features, --features features
111 Space or comma separated list of features to activate. Features of
112 workspace members may be enabled with package-name/feature-name
113 syntax. This flag may be specified multiple times, which enables
114 all specified features.
115
116 --all-features
117 Activate all available features of all selected packages.
118
119 --no-default-features
120 Do not activate the default feature of the selected packages.
121
122 Compilation Options
123 --target triple
124 Build for the given architecture. The default is the host
125 architecture. The general format of the triple is
126 <arch><sub>-<vendor>-<sys>-<abi>. Run rustc --print target-list for
127 a list of supported targets.
128
129 This may also be specified with the build.target config value
130 <https://doc.rust-lang.org/cargo/reference/config.html>.
131
132 Note that specifying this flag makes Cargo run in a different mode
133 where the target artifacts are placed in a separate directory. See
134 the build cache
135 <https://doc.rust-lang.org/cargo/guide/build-cache.html>
136 documentation for more details.
137
138 -r, --release
139 Build optimized artifacts with the release profile. See also the
140 --profile option for choosing a specific profile by name.
141
142 --profile name
143 Build with the given profile.
144
145 The rustc subcommand will treat the following named profiles with
146 special behaviors:
147
148 • check — Builds in the same way as the cargo-check(1) command
149 with the dev profile.
150
151 • test — Builds in the same way as the cargo-test(1) command,
152 enabling building in test mode which will enable tests and
153 enable the test cfg option. See rustc tests
154 <https://doc.rust-lang.org/rustc/tests/index.html> for more
155 detail.
156
157 • bench — Builds in the same was as the cargo-bench(1) command,
158 similar to the test profile.
159
160 See the the reference
161 <https://doc.rust-lang.org/cargo/reference/profiles.html> for more
162 details on profiles.
163
164 --ignore-rust-version
165 Build the target even if the selected Rust compiler is older than
166 the required Rust version as configured in the project's
167 rust-version field.
168
169 --timings=fmts
170 Output information how long each compilation takes, and track
171 concurrency information over time. Accepts an optional
172 comma-separated list of output formats; --timings without an
173 argument will default to --timings=html. Specifying an output
174 format (rather than the default) is unstable and requires
175 -Zunstable-options. Valid output formats:
176
177 • html: Write a human-readable file cargo-timing.html to the
178 target/cargo-timings directory with a report of the
179 compilation. Also write a report to the same directory with a
180 timestamp in the filename if you want to look at older runs.
181 HTML output is suitable for human consumption only, and does
182 not provide machine-readable timing data.
183
184 • json (unstable, requires -Zunstable-options): Emit
185 machine-readable JSON information about timing information.
186
187 Output Options
188 --target-dir directory
189 Directory for all generated artifacts and intermediate files. May
190 also be specified with the CARGO_TARGET_DIR environment variable,
191 or the build.target-dir config value
192 <https://doc.rust-lang.org/cargo/reference/config.html>. Defaults
193 to target in the root of the workspace.
194
195 Display Options
196 -v, --verbose
197 Use verbose output. May be specified twice for "very verbose"
198 output which includes extra output such as dependency warnings and
199 build script output. May also be specified with the term.verbose
200 config value
201 <https://doc.rust-lang.org/cargo/reference/config.html>.
202
203 -q, --quiet
204 Do not print cargo log messages. May also be specified with the
205 term.quiet config value
206 <https://doc.rust-lang.org/cargo/reference/config.html>.
207
208 --color when
209 Control when colored output is used. Valid values:
210
211 • auto (default): Automatically detect if color support is
212 available on the terminal.
213
214 • always: Always display colors.
215
216 • never: Never display colors.
217
218 May also be specified with the term.color config value
219 <https://doc.rust-lang.org/cargo/reference/config.html>.
220
221 --message-format fmt
222 The output format for diagnostic messages. Can be specified
223 multiple times and consists of comma-separated values. Valid
224 values:
225
226 • human (default): Display in a human-readable text format.
227 Conflicts with short and json.
228
229 • short: Emit shorter, human-readable text messages. Conflicts
230 with human and json.
231
232 • json: Emit JSON messages to stdout. See the reference
233 <https://doc.rust-lang.org/cargo/reference/external-tools.html#json-messages>
234 for more details. Conflicts with human and short.
235
236 • json-diagnostic-short: Ensure the rendered field of JSON
237 messages contains the "short" rendering from rustc. Cannot be
238 used with human or short.
239
240 • json-diagnostic-rendered-ansi: Ensure the rendered field of JSON
241 messages contains embedded ANSI color codes for respecting
242 rustc's default color scheme. Cannot be used with human or
243 short.
244
245 • json-render-diagnostics: Instruct Cargo to not include rustc
246 diagnostics in in JSON messages printed, but instead Cargo
247 itself should render the JSON diagnostics coming from rustc.
248 Cargo's own JSON diagnostics and others coming from rustc are
249 still emitted. Cannot be used with human or short.
250
251 Manifest Options
252 --manifest-path path
253 Path to the Cargo.toml file. By default, Cargo searches for the
254 Cargo.toml file in the current directory or any parent directory.
255
256 --frozen, --locked
257 Either of these flags requires that the Cargo.lock file is
258 up-to-date. If the lock file is missing, or it needs to be updated,
259 Cargo will exit with an error. The --frozen flag also prevents
260 Cargo from attempting to access the network to determine if it is
261 out-of-date.
262
263 These may be used in environments where you want to assert that the
264 Cargo.lock file is up-to-date (such as a CI build) or want to avoid
265 network access.
266
267 --offline
268 Prevents Cargo from accessing the network for any reason. Without
269 this flag, Cargo will stop with an error if it needs to access the
270 network and the network is not available. With this flag, Cargo
271 will attempt to proceed without the network if possible.
272
273 Beware that this may result in different dependency resolution than
274 online mode. Cargo will restrict itself to crates that are
275 downloaded locally, even if there might be a newer version as
276 indicated in the local copy of the index. See the cargo-fetch(1)
277 command to download dependencies before going offline.
278
279 May also be specified with the net.offline config value
280 <https://doc.rust-lang.org/cargo/reference/config.html>.
281
282 Common Options
283 +toolchain
284 If Cargo has been installed with rustup, and the first argument to
285 cargo begins with +, it will be interpreted as a rustup toolchain
286 name (such as +stable or +nightly). See the rustup documentation
287 <https://rust-lang.github.io/rustup/overrides.html> for more
288 information about how toolchain overrides work.
289
290 -h, --help
291 Prints help information.
292
293 -Z flag
294 Unstable (nightly-only) flags to Cargo. Run cargo -Z help for
295 details.
296
297 Miscellaneous Options
298 -j N, --jobs N
299 Number of parallel jobs to run. May also be specified with the
300 build.jobs config value
301 <https://doc.rust-lang.org/cargo/reference/config.html>. Defaults
302 to the number of CPUs.
303
304 --keep-going
305 Build as many crates in the dependency graph as possible, rather
306 than aborting the build on the first one that fails to build.
307 Unstable, requires -Zunstable-options.
308
309 --future-incompat-report
310 Displays a future-incompat report for any future-incompatible
311 warnings produced during execution of this command
312
313 See cargo-report(1)
314
316 See the reference
317 <https://doc.rust-lang.org/cargo/reference/environment-variables.html>
318 for details on environment variables that Cargo reads.
319
321 • 0: Cargo succeeded.
322
323 • 101: Cargo failed to complete.
324
326 1. Check if your package (not including dependencies) uses unsafe
327 code:
328
329 cargo rustc --lib -- -D unsafe-code
330
331 2. Try an experimental flag on the nightly compiler, such as this
332 which prints the size of every type:
333
334 cargo rustc --lib -- -Z print-type-sizes
335
337 cargo(1), cargo-build(1), rustc(1)
338
339
340
341 CARGO-RUSTC(1)