CARGO-RUN(1)

hey bestie πŸ’– welcome to cargo-run(1) β€” the manual page for the one command you're gonna type like eight hundred times a day. buckle up, we're going through every single flag, no skips, no skimming, periodt.


πŸŽ€ NAME πŸŽ€

cargo-run β€” run the current package ✨


πŸ¦‹ SYNOPSIS πŸ¦‹

cargo run [options] [-- args]

🌸 DESCRIPTION 🌸

Runs a binary or an example from the local package. that's the whole vibe. main character energy for real.

Every argument that comes after the two dashes (--) gets handed straight to the binary you're running. and if you're passing args to both Cargo and the binary? the ones after -- go to the binary, the ones before -- go to Cargo. iykyk 🫢

Unlike cargo-test(1) and cargo-bench(1), cargo run sets the working directory of the binary it executes to the current working directory β€” same as if you'd just run it in the shell directly. no sneaky directory switching, we respect your cwd πŸ’…


✨ OPTIONS ✨

πŸ“¦ Package Selection

By default, the package in the current working directory is the one that gets selected. but if you want a different package in a workspace, the -p flag is right there bestie.

  • -p spec, --package spec β€” the package to run. see cargo-pkgid(1) for the SPEC format.

🎯 Target Selection

When you don't give any target selection options, cargo run will run the binary target. if there are multiple binary targets, you must pass a target flag to choose one. alternatively, the default-run field can be specified in the [package] section of Cargo.toml to choose the name of the binary that runs by default. iconic ✨

  • --bin name β€” run the specified binary.
  • --example name β€” run the specified example.

🌈 Feature Selection

The feature flags are how you control which features get enabled. when no feature options are given, the default feature is activated for every selected package.

More deets live in the features documentation at https://doc.rust-lang.org/cargo/reference/features.html#command-line-feature-options.

  • -F features, --features features β€” a space or comma separated list of features to activate. features of workspace members can be enabled with the package-name/feature-name syntax. this flag may be specified multiple times, which enables all the specified features. stack them like bracelets πŸŽ€
  • --all-features β€” activate all available features of all selected packages. go big or go home.
  • --no-default-features β€” do not activate the default feature of the selected packages.

πŸ”₯ Compilation Options

  • --target triple β€” run for the specified target architecture. the default is the host architecture. the general format of the triple is <arch><sub>-<vendor>-<sys>-<abi>.

Possible values:

  • Any supported target in rustc --print target-list.
  • "host-tuple", which will internally be substituted by the host's target. this can be particularly useful if you're cross-compiling some crates, and don't want to specify your host's machine as a target (for instance, an xtask in a shared project that may be worked on by many hosts).
  • A path to a custom target specification. see Custom Target Lookup Path at https://doc.rust-lang.org/rustc/targets/custom.html#custom-target-lookup-path for more information.

This may also be specified with the build.target config value at https://doc.rust-lang.org/cargo/reference/config.html.

heads up bestie ⚠️ specifying this flag makes Cargo run in a different mode where the target artifacts are placed in a separate directory. see the build cache docs at https://doc.rust-lang.org/cargo/reference/build-cache.html for more details.
  • -r, --release β€” run optimized artifacts with the release profile. see also the --profile option for choosing a specific profile by name.
  • --profile name β€” run with the given profile. see the reference at https://doc.rust-lang.org/cargo/reference/profiles.html for more details on profiles.
  • --timings β€” output information about how long each compilation takes, and track concurrency information over time.

A file cargo-timing.html will be written to the target/cargo-timings directory at the end of the build. an additional report with a timestamp in its filename is also written if you want to look at a previous run. these reports are suitable for human consumption only, and do not provide machine-readable timing data. look at it with your eyeballs, not your parser πŸ‘€

πŸ“ Output Options

  • --target-dir directory β€” directory for all generated artifacts and intermediate files. may also be specified with the CARGO_TARGET_DIR environment variable, or the build.target-dir config value at https://doc.rust-lang.org/cargo/reference/config.html. defaults to target in the root of the workspace.

πŸ’¬ Display Options

  • -v, --verbose β€” use verbose output. may be specified twice for "very verbose" output which includes extra output such as dependency warnings and build script output. may also be specified with the term.verbose config value at https://doc.rust-lang.org/cargo/reference/config.html.
  • -q, --quiet β€” do not print cargo log messages. may also be specified with the term.quiet config value at https://doc.rust-lang.org/cargo/reference/config.html.
  • --color when β€” control when colored output is used. valid values:
    • auto (default): automatically detect if color support is available on the terminal.
    • always: always display colors.
    • never: never display colors.

May also be specified with the term.color config value at https://doc.rust-lang.org/cargo/reference/config.html.

  • --message-format fmt β€” the output format for diagnostic messages. can be specified multiple times and consists of comma-separated values. valid values:
    • human (default): display in a human-readable text format. conflicts with short and json.
    • short: emit shorter, human-readable text messages. conflicts with human and json.
    • json: emit JSON messages to stdout. see the reference at https://doc.rust-lang.org/cargo/reference/external-tools.html#json-messages for more details. conflicts with human and short.
    • json-diagnostic-short: ensure the rendered field of JSON messages contains the "short" rendering from rustc. cannot be used with human or short.
    • json-diagnostic-rendered-ansi: ensure the rendered field of JSON messages contains embedded ANSI color codes for respecting rustc's default color scheme. cannot be used with human or short.
    • json-render-diagnostics: instruct Cargo to not include rustc diagnostics in JSON messages printed, but instead Cargo itself should render the JSON diagnostics coming from rustc. Cargo's own JSON diagnostics and others coming from rustc are still emitted. cannot be used with human or short.

πŸ“œ Manifest Options

  • --manifest-path path β€” path to the Cargo.toml file. by default, Cargo searches for the Cargo.toml file in the current directory or any parent directory.
  • --ignore-rust-version β€” ignore rust-version specification in packages.
  • --locked β€” asserts that the exact same dependencies and versions are used as when the existing Cargo.lock file was originally generated. Cargo will exit with an error when either of the following scenarios arises:
    • The lock file is missing.
    • Cargo attempted to change the lock file due to a different dependency resolution.

It may be used in environments where deterministic builds are desired, such as in CI pipelines. CI besties, this one's for you 🫢

  • --offline β€” prevents Cargo from accessing the network for any reason. without this flag, Cargo will stop with an error if it needs to access the network and the network is not available. with this flag, Cargo will attempt to proceed without the network if possible.
beware ⚠️ this may result in different dependency resolution than online mode. Cargo will restrict itself to crates that are downloaded locally, even if there might be a newer version as indicated in the local copy of the index. see the cargo-fetch(1) command to download dependencies before going offline.

May also be specified with the net.offline config value at https://doc.rust-lang.org/cargo/reference/config.html.

  • --frozen β€” equivalent to specifying both --locked and --offline. two-for-one deal, slay πŸ’…

🧚 Common Options

  • +toolchain β€” if Cargo has been installed with rustup, and the first argument to cargo begins with +, it will be interpreted as a rustup toolchain name (such as +stable or +nightly). see the rustup documentation at https://rust-lang.github.io/rustup/overrides.html for more information about how toolchain overrides work.
  • --config KEY=VALUE or PATH β€” overrides a Cargo configuration value. the argument should be in TOML syntax of KEY=VALUE, or provided as a path to an extra configuration file. this flag may be specified multiple times. see the command-line overrides section at https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides for more information.
  • -C PATH β€” changes the current working directory before executing any specified operations. this affects things like where cargo looks by default for the project manifest (Cargo.toml), as well as the directories searched for discovering .cargo/config.toml, for example. this option must appear before the command name, for example cargo -C path/to/my-project build.
this option is only available on the nightly channel at https://doc.rust-lang.org/book/appendix-07-nightly-rust.html and requires the -Z unstable-options flag to enable (see #10098 at https://github.com/rust-lang/cargo/issues/10098). nightly-only, bestie, don't try this on stable 😭
  • -h, --help β€” prints help information.
  • -Z flag β€” unstable (nightly-only) flags to Cargo. run cargo -Z help for details.

🌟 Miscellaneous Options

  • -j N, --jobs N β€” number of parallel jobs to run. may also be specified with the build.jobs config value at https://doc.rust-lang.org/cargo/reference/config.html. defaults to the number of logical CPUs. if negative, it sets the maximum number of parallel jobs to the number of logical CPUs plus the provided value. if a string default is provided, it sets the value back to defaults. should not be 0. like, seriously, don't.
  • --keep-going β€” build as many crates in the dependency graph as possible, rather than aborting the build on the first one that fails to build.

For example, if the current package depends on dependencies fails and works, one of which fails to build, cargo run -j1 may or may not build the one that succeeds (depending on which one of the two builds Cargo picked to run first), whereas cargo run -j1 --keep-going would definitely run both builds, even if the one run first fails. resilience era πŸ’ͺ


🌸 ENVIRONMENT 🌸

ok so Cargo reads environment variables, and if you wanna know exactly which ones and what they do, bestie, the receipts are all here:

<https://doc.rust-lang.org/cargo/reference/environment-variables.html>

that's the official reference, go read it, it's giving thorough πŸ’…


✨ EXIT STATUS ✨

Cargo keeps it super simple, only two moods:

  • 0 β€” Cargo succeeded. slay, we ate, everything worked πŸŽ€
  • 101 β€” Cargo failed to complete. not the vibe, something went wrong 😭

πŸ¦‹ EXAMPLES πŸ¦‹

okay real talk, here's how you actually use this thing:

  1. build the local package and run its main target β€” this assumes there's only one binary, iykyk:
cargo run
  1. run an example, and pass it some extra args while you're at it β€” note the -- bestie, that's the little wall between Cargo's args and your program's args, do not forget it or it's over:
cargo run --example exname -- --exoption exarg1 exarg2

like, see how --example exname is for Cargo and everything after the -- goes straight to your example? that's the whole trick πŸ‘‘


πŸ‘‘ SEE ALSO πŸ‘‘


ok that's the whole page, you're officially a cargo run girly now, no cap πŸ’– this was CARGO-RUN(1) from the Cargo manual, mwah, go build something cute 🫢✨

πŸ“¦ where this page comes from