FAQ - Help and questions

What is Cargo, and what does it do for me?

#cargo#basics#toolingpublished 0 replies0 views
ReplyWatch
Share
Hacker NewsRedditLinkedInE-mail

Short answer

Cargo is Rust's build tool and package manager. One command creates a project, downloads and builds its dependencies from crates.io, compiles, runs and tests it. Project settings and dependencies live in Cargo.toml, and Cargo.lock records the exact versions that were built.

Checked with cargo 1.93.0, Windows 11

Frequently asked question, written and answered by the Ruststudio team. Replies are open: corrections are welcome.

#1

Answer#

Cargo is installed with Rust (through rustup) and is the command you use for almost everything: creating projects, adding libraries (called crates), building, running, testing and publishing. You rarely call the compiler, rustc, by hand.

A project is a folder with a Cargo.toml file (its description and dependencies) and a src folder. Cargo also writes Cargo.lock, which pins the exact version of every dependency, and a target folder that holds everything it builds.

How to do it#

  1. Create a project. This also starts a Git repository in the folder unless it is already inside one.
sh
   cargo new hello

This is the Cargo.toml it writes with cargo 1.93.0:

Cargo.tomltoml
   [package]
   name = "hello"
   version = "0.1.0"
   edition = "2024"

   [dependencies]
  1. Build and run it. The first command compiles in the fast-to-build debug profile; target/debug holds the result.
sh
   cargo run
text
   Hello, world!
  1. Run the tests (there are none yet, so the result is empty but passes).
sh
   cargo test
  1. Add a dependency. The command edits Cargo.toml for you and picks the newest compatible version.
sh
   cargo add serde_json
  1. Check the code without producing a program. This is much faster than a build and reports the same errors.
sh
   cargo check
  1. Build an optimised program for release. It is placed in target/release.
sh
   cargo build --release
CommandWhat it does
cargo new, cargo initCreate a project in a new or existing folder
cargo checkType-check without producing a program
cargo build, cargo build --releaseCompile (debug, or optimised)
cargo runBuild, then run
cargo testBuild and run the tests
cargo add, cargo removeEdit the dependency list
cargo treeShow the dependency graph
cargo updateMove Cargo.lock to newer versions
cargo cleanDelete the target folder

Common mistakes#

  • Committing the target folder to Git. It is large and rebuildable; the .gitignore that cargo new writes already excludes it.
  • Forgetting --release when measuring speed. Debug builds are often many times slower.
  • Never cleaning old target folders. Each project keeps its own, and they grow large over time.

In Ruststudio#

Ruststudio runs these commands for you from its menus and shows their output, and its Cargo Cleaner finds the target folders and cargo caches on your disk, with their size and age, and deletes only what you tick. See the Cargo Cleaner page.

What is the difference between Cargo.toml and Cargo.lock?#

Cargo.toml is what you write: the dependencies you want, with version ranges. Cargo.lock is what Cargo writes: the exact versions it chose. Commit Cargo.lock for programs so that every build uses the same versions.

What is a crate?#

A crate is one unit of Rust code that is compiled together: a library or a program. Libraries from crates.io are crates, and so is your own program.

Sources#

    QuoteReply
    Share
    Hacker NewsRedditLinkedInE-mail

    No replies yet