Environment Variables
Cargo sets and reads a number of environment variables which your code can detector override. Here is a list of the variables Cargo sets, organized by when it interactswith them:
Environment variables Cargo reads
You can override these environment variables to change Cargo's behavior on yoursystem:
CARGO_HOME
— Cargo maintains a local cache of the registry index and of gitcheckouts of crates. By default these are stored under$HOME/.cargo
, butthis variable overrides the location of this directory. Once a crate is cachedit is not removed by the clean command.For more details refer to the guide.CARGO_TARGET_DIR
— Location of where to place all generated artifacts,relative to the current working directory.RUSTC
— Instead of runningrustc
, Cargo will execute this specifiedcompiler instead.RUSTC_WRAPPER
— Instead of simply runningrustc
, Cargo will execute thisspecified wrapper instead, passing as its commandline arguments the rustcinvocation, with the first argument being rustc.RUSTDOC
— Instead of runningrustdoc
, Cargo will execute this specifiedrustdoc
instance instead.RUSTDOCFLAGS
— A space-separated list of custom flags to pass to allrustdoc
invocations that Cargo performs. In contrast withcargo rustdoc
, this isuseful for passing a flag to allrustdoc
instances.RUSTFLAGS
— A space-separated list of custom flags to pass to all compilerinvocations that Cargo performs. In contrast withcargo rustc
, this isuseful for passing a flag to all compiler instances.CARGO_INCREMENTAL
— If this is set to 1 then Cargo will force incrementalcompilation to be enabled for the current compilation, and when set to 0 itwill force disabling it. If this env var isn't present then cargo's defaultswill otherwise be used.CARGO_CACHE_RUSTC_INFO
— If this is set to 0 then Cargo will not try to cachecompiler version information.Note that Cargo will also read environment variables for.cargo/config
configuration values, as described in that documentation
Environment variables Cargo sets for crates
Cargo exposes these environment variables to your crate when it is compiled.Note that this applies for test binaries as well.To get the value of any of these variables in a Rust program, do this:
# #![allow(unused_variables)]
#fn main() {
let version = env!("CARGO_PKG_VERSION");
#}
version
will now contain the value of CARGO_PKG_VERSION
.
CARGO
- Path to thecargo
binary performing the build.CARGO_MANIFEST_DIR
- The directory containing the manifest of your package.CARGO_PKG_VERSION
- The full version of your package.CARGO_PKG_VERSION_MAJOR
- The major version of your package.CARGO_PKG_VERSION_MINOR
- The minor version of your package.CARGO_PKG_VERSION_PATCH
- The patch version of your package.CARGO_PKG_VERSION_PRE
- The pre-release version of your package.CARGO_PKG_AUTHORS
- Colon separated list of authors from the manifest of your package.CARGO_PKG_NAME
- The name of your package.CARGO_PKG_DESCRIPTION
- The description from the manifest of your package.CARGO_PKG_HOMEPAGE
- The home page from the manifest of your package.CARGO_PKG_REPOSITORY
- The repository from the manifest of your package.OUT_DIR
- If the package has a build script, this is set to the folder where the buildscript should place its output. See below for more information.
Environment variables Cargo sets for build scripts
Cargo sets several environment variables when build scripts are run. Because these variablesare not yet set when the build script is compiled, the above example using env!
won't workand instead you'll need to retrieve the values when the build script is run:
# #![allow(unused_variables)]
#fn main() {
use std::env;
let out_dir = env::var("OUT_DIR").unwrap();
#}
out_dir
will now contain the value of OUT_DIR
.
CARGO
- Path to thecargo
binary performing the build.CARGO_MANIFEST_DIR
- The directory containing the manifest for the packagebeing built (the package containing the buildscript). Also note that this is the value of thecurrent working directory of the build script when itstarts.CARGO_MANIFEST_LINKS
- the manifestlinks
value.CARGOFEATURE<name>
- For each activated feature of the package beingbuilt, this environment variable will be presentwhere<name>
is the name of the feature uppercasedand having-
translated to_
.CARGOCFG<cfg>
- For each configuration option of thepackage being built, this environment variable willcontain the value of the configuration, where<cfg>
isthe name of the configuration uppercased and having-
translated to_
.Boolean configurations are present if they are set, andnot present otherwise.Configurations with multiple values are joined to asingle variable with the values delimited by,
.OUT_DIR
- the folder in which all output should be placed. This folder isinside the build directory for the package being built, and it isunique for the package in question.TARGET
- the target triple that is being compiled for. Native code should becompiled for this triple. See the Target Triple descriptionfor more information.HOST
- the host triple of the rust compiler.NUM_JOBS
- the parallelism specified as the top-level parallelism. This canbe useful to pass a-j
parameter to a system likemake
. Notethat care should be taken when interpreting this environmentvariable. For historical purposes this is still provided butrecent versions of Cargo, for example, do not need to runmake -j
as it'll automatically happen. Cargo implements its ownjobserver and will allow build scripts to inherit thisinformation, so programs compatible with GNU make jobservers willalready have appropriately configured parallelism.OPT_LEVEL
,DEBUG
- values of the corresponding variables for theprofile currently being built.PROFILE
-release
for release builds,debug
for other builds.DEP<name><key>
- For more information about this set of environmentvariables, see build script documentation aboutlinks
.RUSTC
,RUSTDOC
- the compiler and documentation generator that Cargo hasresolved to use, passed to the build script so it mightuse it as well.RUSTC_LINKER
- The path to the linker binary that Cargo has resolved to usefor the current target, if specified. The linker can bechanged by editing.cargo/config
; see the documentationabout cargo configuration for moreinformation.
Environment variables Cargo sets for 3rd party subcommands
Cargo exposes this environment variable to 3rd party subcommands(ie. programs named cargo-foobar
placed in $PATH
):
CARGO
- Path to thecargo
binary performing the build.