cargo-run
Run the Rust project's main executable
TLDR
Run the default binary target
Run the specified binary
Run the specified example
Activate a space or comma separated list of features
Disable the default features
Activate all available features
Run with the given profile
SYNOPSIS
cargo run [OPTIONS] [-- args...]
PARAMETERS
--bin NAME
Run the specified binary. Use this when your package has multiple binary targets defined in Cargo.toml.
--example NAME
Run the specified example. Use this when your package has example targets defined in Cargo.toml.
--package SPEC, -p SPEC
Run a binary or example from the specified package. This is useful in a workspace to specify which package to run.
--release
Compile and run the optimized release build (located in target/release/), rather than the default debug build (in target/debug/).
--target TRIPLE
Build for the specified target triple (e.g., x86_64-unknown-linux-gnu).
--features FEATURES
Space or comma separated list of features to activate for compilation.
--all-features
Activate all available features of the current package.
--no-default-features
Do not activate the default feature of the current package.
--jobs N, -j N
Number of parallel jobs to run for compilation.
--verbose, -v
Use verbose output, showing more details like executed commands.
--quiet, -q
Do not print Cargo log messages, only program output and errors.
--manifest-path PATH
Path to the Cargo.toml file for the package being run.
--color WHEN
Control when coloring is used in the output. Options include always, auto, never.
DESCRIPTION
cargo run is a convenient command provided by Cargo, Rust's build system and package manager. It streamlines the development workflow by performing two primary actions:
1. It first compiles the current package (or a specific binary, example, or test as specified by options) into an executable.
2. Upon successful compilation, it then executes the generated binary.
This command is particularly useful during rapid development cycles, allowing developers to quickly test changes without manually compiling and then invoking the executable. It automatically handles dependency resolution and compilation, only rebuilding what's necessary based on source file changes. If multiple runnable targets (like multiple binaries or examples) are found within a package and none is specified via options, cargo run will typically prompt the user to choose or fail with an ambiguity error.
CAVEATS
When using cargo run, it's crucial to understand the -- separator. Any arguments appearing after -- are passed directly to the executed program, not to cargo run itself. For example, cargo run -- --some-arg value passes --some-arg value to your Rust binary.
If multiple binary or example targets are defined in your Cargo.toml and you do not specify which one to run (e.g., with --bin or --example), cargo run might display an error or prompt you to select one, leading to potential ambiguity in automated scripts.
PASSING ARGUMENTS TO YOUR PROGRAM
To pass arguments to the compiled executable, you must use -- as a separator between cargo run's options and your program's arguments.
Example: cargo run -- --input data.txt --verbose
Here, --input data.txt --verbose will be received by your Rust program.
TARGET SELECTION AND AMBIGUITY
If your Cargo.toml defines multiple binaries (e.g., src/bin/my_app.rs and src/bin/another_app.rs) or examples, cargo run will require you to explicitly choose which one to run using --bin
HISTORY
cargo run has been a fundamental command within the Cargo build system since its inception, predating the stable release of Rust 1.0. Its purpose has consistently been to provide a seamless 'build and execute' cycle, drastically improving developer productivity by abstracting away the manual compilation steps. While new options have been added over time to support features like workspaces, profiles, and more granular target selection, the core functionality and user experience of cargo run have remained remarkably consistent, establishing it as a go-to command for everyday Rust development.
SEE ALSO
cargo(1), cargo-build(1), cargo-check(1), cargo-test(1), rustc(1)