Skip to main content

How to Run a Rust Program with cargo run

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

cargo run builds a Rust program when its code has changed and then runs it, all in one command. A few options cover almost everything else: -q to hide Cargo's own messages, -- to pass arguments to your program, and --release for a fast, optimized build.

Syntax​

Syntax
cargo run [-q] [--release] [-- <arguments>]
PartWhat it does
cargo runCompiles the project in the folder you're in if anything changed, then runs it.
-qOptional. Hides Cargo's status lines, so only the program's output is left.
--releaseOptional. Builds an optimized program, slower to compile but faster to run.
-- <arguments>Optional. Everything after -- goes to your program instead of to Cargo.

The Example​

The example program greets the name it receives as an argument, or the whole world when it receives none.

Prerequisites​

  • Rust, installed with rustup, which comes with Cargo. This guide uses Cargo 1.100.0.

Step 1: Create the Program​

Terminal
cargo new hello-cargo
cd hello-cargo

How to Create a Rust Project with cargo new explains the first command. Replace src/main.rs with:

src/main.rs, which takes the first argument from env::args, falling back to world, and prints Hello with the name and This is cargo run.

src/main.rs
use std::env;

fn main() {
let name = env::args().nth(1).unwrap_or_else(|| "world".to_string());
println!("Hello, {name}! This is cargo run.");
}

env::args() lists the command line, with the program's own path first, so .nth(1) is the first argument. unwrap_or_else uses "world" when there's none.

Step 2: Run It​

Terminal
cargo run
Output
Compiling hello-cargo v0.1.0 (/Users/Shared/you/hello-cargo)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.40s
Running `target/debug/hello-cargo`
Hello, world! This is cargo run.
LineWhat it means
CompilingCargo compiled the project, because its code is new.
Finished `dev` profile [unoptimized + debuginfo]It built with the dev profile: quick to compile, not optimized, with debugging information.
Running `target/debug/hello-cargo` The program Cargo started. It stays there, so you can run it again without Cargo.

The last line is your program's output.

Step 3: Pass an Argument, Quietly​

Terminal
cargo run -q -- Ada
Output
Hello, Ada! This is cargo run.

A zsh terminal where cargo run compiles and runs hello-cargo, printing Hello, world!, and cargo run -q -- Ada prints only Hello, Ada! This is cargo run.

-q left only the program's own line, and the -- sent Ada to the program. The commands are the same in zsh, bash and PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
Arguments without --, such as cargo run -q -v meant for your programCargo takes options it knows, such as -v, for itselfPut your program's arguments after --
Timing or benchmarking a cargo run buildThe dev profile isn't optimized, so it's slower than it will beUse cargo run --release
Running cargo run outside the projectCargo can't find Cargo.tomlcd into the project first

Troubleshooting​

SymptomLikely causeFix
error: could not find `Cargo.toml` in `…` or any parent directoryYou're not inside the projectcd into the folder with Cargo.toml
The first run takes a long timeCargo compiles every crate the project uses onceLater runs reuse that work and start quickly

FAQ​

What's the difference between cargo run and cargo build?​

cargo build only compiles, into target/debug. cargo run compiles when needed and then runs the program.

Where's the program after cargo run --release?​

In target/release/<name>, next to the target/debug one.

Conclusion​

cargo run builds and runs a Rust program in one step. Add -q to hide Cargo's lines, put your program's arguments after --, and use --release when speed matters.

Additional Resources​