liveBook · Manning

Cargo run

As explained in Rust in Action: Systems programming concepts and techniques

cargo run is a Cargo subcommand that builds a Rust project and executes its resulting binary.

Run `cargo run` from a Rust project’s directory to build and execute its program. Cargo drives the Rust compiler, `rustc`, for you. If the project has not yet been built, the command compiles it first, then runs the resulting executable. section 1.3.1 section 1.6.2

$ cd $TMP 
 
$ cargo new hello 
     Created binary (application) `hello` package
 
$ cd hello 
 
$ cargo run 
   Compiling hello v0.1.0 (/tmp/hello)
    Finished dev [unoptimized + debuginfo] target(s) in 0.26s
     Running `target/debug/hello`
Hello, world!

Try the short path from a new project to a running program. The output separates compilation, the executable’s location, and the program’s own “Hello, world!” message. section 1.3.1

By default, the executable goes under `target/debug/`; on Windows, its name ends in `.exe`. A first run also creates Cargo-managed build artifacts, including `target/` and `Cargo.lock`. The lockfile records exact dependency versions for repeatable future builds. section 1.3.1

Change how the command builds or reports its work:

  • `cargo run --release` builds an optimized executable under `target/release/`. It can run faster, although compilation takes longer. section 1.5
  • `cargo run -q --release` suppresses Cargo’s progress messages, leaving the program’s output. section 1.5
  • `cargo run -v` exposes more of Cargo’s work, including the `rustc` invocation when compilation occurs. section 2.1.2

How Rust in Action: Systems programming concepts and techniques covers it

How deep it goes
Explains build output, generated files, and common flags, then exposes the compiler invocation with verbose output.
Examples
Uses a new Hello World project, a CSV-processing program, and a single-file project migrated to Cargo.
What it assumes
Starts with a generated project and shows where to run the command; assumes access to a console and an installed Rust toolchain.
What you can do after
Operate/configure it to run a project in debug or release mode and adjust the amount of Cargo output.
How it connects
Connects Cargo projects, `cargo build`, `rustc`, debug builds, release builds, and dependency lockfiles.

Also explained in

Compare how 2 books explain cargo run →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust in Action: Systems programming concepts and techniques. The section links point to the parts of the book it draws on.