skip to content

Cargo

Rust's build system and package manager in one tool: manifests, dependency resolution, features, profiles, workspaces and publishing. Interviewers ask how a real Rust project builds.

on this pageshow

explore

questions

64 · 12 sections

In a Cargo.toml [package] table, what do name, version, edition, rust-version and license do, and which key is actually required?

level: juniorimportance: must knowfreq 70%
basics
~20 s

Only name is required: it names the package and its default targets. version defaults to 0.0.0, a missing edition falls back to 2015 with a warning, rust-version is the oldest supported toolchain, and license is metadata a registry needs.

open as a page

In a Cargo package with src/lib.rs, src/main.rs and two files in src/bin/, which targets does Cargo build, and how do the binaries reach the library?

level: middleimportance: must knowfreq 60%
basics
~20 s

Cargo discovers one library from src/lib.rs and a binary from src/main.rs plus one per file in src/bin/. Each binary is a separate crate that links the library and calls its public API by crate name, for example geo_tiles::tiles_per_edge.

open as a page

In a Cargo.toml [lib] table, what do crate-type values lib, rlib, cdylib and staticlib produce, and what breaks when only cdylib is listed?

level: middleimportance: should knowfreq 35%
basics
~20 s

lib (the default) and rlib emit a Rust library for Rust crates; cdylib a shared library for other languages; staticlib an archive bundling every dependency. Listing only cdylib leaves the package's own binaries and tests nothing to link.

open as a page

A Cargo.toml [[bin]] named tile-admin has required-features = ["admin"]; why does cargo build skip it silently, and how do you keep it compiling?

level: seniorimportance: should knowfreq 25%
basics
~20 s

required-features makes Cargo skip the target whenever a listed feature is off, so cargo build without --features admin never compiles it. Only naming it with --bin errors. CI must build with the feature, or the tool silently rots.

open as a page

A Cargo.toml declares rust-version = "1.80", yet users on Rust 1.80 hit compile errors inside the crate; what does rust-version actually make Cargo enforce?

level: seniorimportance: should knowfreq 35%
basics
~20 s

Cargo enforces rust-version as a floor: it refuses a rustc older than the declared version, for the package and its dependencies. It never checks that the code avoids newer APIs, so the claim can silently go false.

open as a page

In a Cargo.toml, which versions may Cargo select for `serde = "1.0.200"`, and why does that line count as a caret requirement?

level: juniorimportance: must knowfreq 74%
basics
~10 s

A bare version in Cargo is a caret requirement, so serde = "1.0.200" means >=1.0.200, <2.0.0. Cargo may pick any release in that range, normally the newest, and records its pick in Cargo.lock.

open as a page

In a Cargo.toml, how do `[dev-dependencies]`, `[build-dependencies]` and `[target.'cfg(...)'.dependencies]` differ from `[dependencies]` in which code may use them?

level: middleimportance: should knowfreq 56%
basics
~10 s

Normal dependencies serve the package and its dependents; dev-dependencies only tests, examples and benchmarks; build-dependencies only build.rs; and a target cfg table adds dependencies only when compiling for a matching platform.

open as a page

In a Cargo.toml, what does the caret rule allow for 0.x and 0.0.x versions, and what do tilde, wildcard and `=` requirements change?

level: middleimportance: should knowfreq 60%
basics
~10 s

Cargo fixes the left-most non-zero component, so "0.3.1" means >=0.3.1, <0.4.0 and "0.0.4" means >=0.0.4, <0.0.5. Tilde and "1.2.*" fix the minor version, and "=1.2.3" accepts that one release.

open as a page

A Rust crate's Cargo.toml has a git dependency pinned by `rev` and a path dependency on a sibling crate; how does Cargo source each, and what must change before `cargo publish`?

level: seniorimportance: should knowfreq 34%
basics
~20 s

Cargo fetches the git repository at the given rev and builds the path crate from that folder. Publishing needs a version on every non-dev dependency: git and path keys are stripped and registry versions used, so each must exist in the registry.

open as a page

In a Cargo.toml, what does `json = { package = "serde_json", version = "1" }` change, and when is renaming a dependency worth doing?

level: middleimportance: nice to knowfreq 24%
basics
~20 s

The package key names the real crate and the table key becomes the name code uses: this line depends on serde_json but code writes json::from_str. Renaming lets one package use two versions or two same-named crates side by side.

open as a page

In a Cargo project, what does Cargo.lock record that a requirement like `serde_json = "1.0"` in Cargo.toml does not, and should you commit it?

level: juniorimportance: must knowfreq 72%
basics
~20 s

Cargo.toml states a range ("1.0" means >=1.0.0, <2.0.0); Cargo.lock records the exact version Cargo resolved for every package, with source and checksum for registry crates, so later builds reuse it. Cargo's guidance: when in doubt, commit it.

open as a page

In Cargo, crates require `toml = "1.1"`, `"1.0"` and `"0.9"`; how many toml copies get built, and which command shows why?

level: middleimportance: must knowfreq 58%
basics
~20 s

Two copies. "1.1" and "1.0" share the compatible range >=1.0.0, <2.0.0, so Cargo unifies them on one 1.x version; "0.9" means >=0.9.0, <0.10.0, a separate range. cargo tree -d lists every crate built in several versions and who pulls each.

open as a page

With Cargo, how do you move only one transitive dependency in Cargo.lock to a fixed release, and what if no dependent's range allows it?

level: middleimportance: must knowfreq 52%
basics
~20 s

Run cargo update -p NAME (a conservative update that moves only that package) or add --precise VERSION to pin the fixed release. If a dependent's requirement excludes the fix, upgrade that dependent or add a [patch.crates-io] entry pointing at a fixed copy.

open as a page

A Cargo build fails with E0308 "expected `Map<String, Value>`, found a different `Map<String, Value>`" between your code and a dependency; what happened and how do you fix it?

level: seniorimportance: should knowfreq 45%
basics
~20 s

Two SemVer-incompatible versions of toml are in the graph, and rustc treats each version's types as unrelated, so a toml::Table from 0.9 cannot be passed where your 1.x toml::Table is expected. Align both sides on one major, or convert at the boundary.

open as a page

Your Cargo package sets `rust-version = "1.85"` on edition 2024; why does Cargo lock an older dependency release than resolver "2" would, and how do you override it?

level: seniorimportance: should knowfreq 30%
basics
~20 s

Edition 2024 implies resolver "3", the MSRV-aware resolver stabilised in Cargo 1.84: it prefers dependency releases whose rust-version is at most yours, falling back to newer ones only when none fit. Override with resolver.incompatible-rust-versions = "allow", --ignore-rust-version or cargo update --precise.

open as a page

In a Cargo.toml, what do `default` in `[features]`, `--no-default-features` and `default-features = false` each control?

level: middleimportance: must knowfreq 65%
basics
~20 s

The default feature lists what Cargo turns on automatically. --no-default-features switches it off for the packages built from the command line; default-features = false does the same in a dependency entry, but another dependent can still re-enable them.

open as a page

In a Cargo build, why does an app enabling only `tls-native` on db-client, with `default-features = false`, still hit the crate's both-backends `compile_error!`?

level: seniorimportance: must knowfreq 55%
basics
~20 s

Cargo builds db-client once with the union of every dependent's features. Another crate in the graph depends on it with defaults, so tls-rustls is on beside tls-native, which is why Cargo features must be additive and never mutually exclusive.

open as a page

In a Cargo.toml, how do `optional = true`, `"dep:serde"` and `"serde?/std"` differ in what a feature turns on?

level: middleimportance: should knowfreq 50%
basics
~20 s

An optional dependency is left out of the build and gets an implicit same-named feature. Writing dep:serde anywhere in [features] removes that implicit feature, and serde?/std enables serde's std only if something else already pulled serde in.

open as a page

Why does a no_std firmware crate on Cargo's resolver "1" fail to cross-compile when only its build-dependency enables serde's std, and what does resolver "2" change?

level: seniorimportance: should knowfreq 40%
basics
~20 s

Resolver 1 unifies features across all dependency kinds, so the build script's std request also lands on the firmware's serde, which cannot build without std. Resolver 2 keeps build-dependencies, proc-macros, inactive dev-dependencies and other platforms' dependencies apart.

open as a page

As maintainer of a Rust database-client crate, how would you design its Cargo features and defaults so downstream builds stay predictable?

level: principalimportance: should knowfreq 35%
basics
~20 s

Keep features additive and named for capabilities, make the default set small but useful, hide optional dependencies behind dep:, use weak ?/ for cross-features, and build no-default, default and all-features in CI, since removing a feature is breaking.

open as a page

In Cargo, what changes when you run `cargo build --release` instead of `cargo build`, and which profile settings cause it?

level: juniorimportance: must knowfreq 75%
basics
~20 s

Plain cargo build uses the dev profile: opt-level 0, full debug info, debug assertions and overflow checks on, incremental. The --release flag selects the release profile: opt-level 3, no debug info, both checks off, so builds are slower and binaries faster.

open as a page

A Rust command-line tool's release binary is 2.5 MB; which Cargo profile settings make it smaller, and what does each one cost?

level: middleimportance: must knowfreq 60%
basics
~20 s

In the release profile: lto = true, codegen-units = 1, panic = "abort", strip = true and opt-level "s" or "z", each measured alone. They cost build time, backtraces or panic recovery; since Cargo 1.77 debuginfo is already stripped by default.

open as a page

A Rust game is unplayably slow in Cargo's dev profile; how do profile overrides speed it up while keeping your own crates easy to debug?

level: middleimportance: should knowfreq 45%
basics
~20 s

Add [profile.dev.package."*"] with opt-level = 3 to the root Cargo.toml: every non-member dependency is optimised once, while your workspace crates stay at opt-level 0 with full debug info. Raise [profile.dev] opt-level to 1 if your own code is also too slow.

open as a page

In a Cargo workspace, a member crate's Cargo.toml sets `[profile.release]` with `lto = true`, yet release builds show no LTO; why, and how do you fix it?

level: seniorimportance: should knowfreq 35%
basics
~20 s

Cargo reads profiles only from the workspace root manifest and ignores a member's [profile] tables, printing a warning. Move the table to the root Cargo.toml, or override it through config or CARGO_PROFILE_RELEASE_LTO, then confirm -C lto with cargo build --release -v.

open as a page

As lead of a Rust service team, which Cargo release profile would you standardise — LTO, codegen-units, panic strategy, debug info — and why?

level: principalimportance: should knowfreq 40%
basics
~20 s

Keep release near its defaults for local builds and ship from a custom profile inheriting release: LTO and codegen-units = 1 chosen by measurement, debug = "line-tables-only" with split debug info, and panic left at unwind unless abort is deliberate.

open as a page

In a Cargo workspace, how does a member inherit a dependency from [workspace.dependencies], and what may the member still change on that entry?

level: middleimportance: must knowfreq 50%
basics
~20 s

The root declares the requirement once in [workspace.dependencies]; a member opts in with serde.workspace = true. The member may add features, mark it optional and, since Cargo 1.99 on edition 2024, turn default features off, but not change its version.

open as a page

In Cargo, what does a workspace holding api, core and cli crates share across its members that three standalone packages would each keep separately?

level: juniorimportance: should knowfreq 48%
basics
~20 s

A Cargo workspace gives its member packages one Cargo.lock and one target directory at the workspace root, lets one command build or test every member, and reads profiles and [patch] only from the root manifest.

open as a page

In a Cargo virtual manifest, which packages does a bare cargo build at the root compile, and why must that manifest set resolver itself?

level: middleimportance: should knowfreq 38%
basics
~20 s

A virtual manifest has [workspace] and no [package]; a bare cargo build at its root builds every member, or default-members if set. With no edition to infer from, it must set resolver, or Cargo falls back to resolver 1.

open as a page

As a lead, how do you decide how finely to split a Rust codebase into crates in a Cargo workspace, and what does each extra crate cost?

level: principalimportance: should knowfreq 27%
basics
~20 s

Split where it buys something measurable: crates Cargo can compile in parallel, a stable base that edits above it do not rebuild, isolated heavy dependencies, enforced API boundaries. Each crate costs a manifest, a public API, possible orphan-rule friction and lost inlining.

open as a page

With Cargo, what do `cargo build`, `cargo run` and `--release` each do, and where does each one leave the compiled binary?

level: juniorimportance: must knowfreq 62%
basics
~10 s

cargo build compiles the package with the dev profile into target/debug; cargo run does the same build and then executes the binary; --release switches either command to the optimized release profile and target/release.

open as a page

Why is `cargo check` faster than `cargo build`, and which kinds of errors can `cargo check` miss?

level: middleimportance: must knowfreq 58%
basics
~20 s

cargo check runs the compiler's analysis, including type and borrow checking, but skips code generation and linking, emitting only metadata. Errors that surface during code generation or linking, such as a missing native symbol, appear only with cargo build.

open as a page

What does `cargo install` build, where does Cargo put the installed binaries, and why does `--locked` matter there?

level: middleimportance: should knowfreq 32%
basics
~20 s

cargo install builds a package's binaries with the release profile and copies them into the installation root's bin folder, $HOME/.cargo/bin by default. It ignores the package's own Cargo.lock and resolves dependencies afresh unless you pass --locked.

open as a page

With `cargo test`, how do you run a single test by name, see its `println!` output, and run tests one at a time?

level: middleimportance: should knowfreq 50%
basics
~20 s

Pass the name as a filter and put harness options after the double dash: cargo test counts_spaces -- --no-capture --test-threads=1. The filter matches substrings of test paths, and options after -- go to each test binary, not to Cargo.

open as a page

A Rust crate's CI runs `cargo build` and `cargo test`, yet a benchmark stopped compiling and `Cargo.lock` drifted unnoticed; which Cargo flags close both gaps?

level: seniorimportance: should knowfreq 38%
basics
~20 s

Neither command builds benches by default, and both rewrite an out-of-date Cargo.lock and carry on. Add --all-targets to compile benches, keep cargo test --doc because --all-targets skips doc tests, and pass --locked so Cargo errors instead of changing the lockfile.

open as a page

In a Rust project's CI job, what do `cargo fmt --check` and `cargo clippy` each verify, and what makes each step fail?

level: juniorimportance: must knowfreq 55%
basics
~20 s

cargo fmt --check runs rustfmt without rewriting files and fails when any file would change. cargo clippy type-checks the crate and runs Clippy's lints; it fails on deny-level lints, while plain warnings pass unless CI promotes them to errors.

open as a page

With Cargo, what does `cargo doc --no-deps` build, and why does a broken intra-doc link pass `cargo clippy` and `cargo test` unnoticed?

level: middleimportance: should knowfreq 25%
basics
~20 s

cargo doc --no-deps runs rustdoc on the selected packages only and writes HTML to target/doc. Intra-doc links are resolved only by rustdoc, and broken_intra_doc_links only warns, so CI must run cargo doc with that lint denied.

open as a page

In Clippy, which lint groups are off by default, and how should a Rust team adopt the `pedantic` and `restriction` groups?

level: middleimportance: should knowfreq 40%
basics
~20 s

Clippy's pedantic, nursery, restriction and cargo groups are allow-by-default. Teams enable pedantic as a group at a lower priority and allow the lints they reject, but pick restriction lints one at a time, because that group contradicts itself.

open as a page

In Rust 1.81 and later, how does `#[expect(clippy::needless_range_loop)]` differ from `#[allow(clippy::needless_range_loop)]`, and when does it warn?

level: middleimportance: should knowfreq 30%
basics
~20 s

Both silence the named lint, but #[expect] also asserts that the lint fires there. When the code changes and the lint stops firing, rustc warns with unfulfilled_lint_expectations, so a stale suppression surfaces instead of lingering like a forgotten #[allow].

open as a page

In a Cargo workspace, why does `cargo clippy` reject `[workspace.lints.clippy]` setting `pedantic = "warn"` beside `cast_possible_truncation = "allow"`, and how do members inherit it?

level: middleimportance: should knowfreq 40%
basics
~20 s

Cargo ignores the order of keys in a lints table, so a group and a lint at the same priority conflict and Clippy's deny-by-default lint_groups_priority fails the run. Give the group priority = -1, and put lints.workspace = true in every member.

open as a page

In Cargo, when does a package's build script rerun, and how do cargo::rerun-if-changed and cargo::rerun-if-env-changed change that rule?

level: middleimportance: must knowfreq 35%
basics
~20 s

With no rerun-if lines, Cargo reruns a build script whenever any file in the package changes. Once the script prints one, Cargo stops that scan and watches what was listed: a path's modification time, or an environment variable's value.

open as a page

In a Cargo package, what is build.rs, when does Cargo run it, and how does it pass instructions back to Cargo?

level: juniorimportance: should knowfreq 30%
basics
~20 s

build.rs is a Rust program in the package root that Cargo compiles and runs just before compiling the package. It talks to Cargo by printing cargo:: lines to stdout, and a panic or non-zero exit stops the build.

open as a page

A Cargo build script generates a CRC-32 lookup table at build time; where must it write the file, and how does the crate include it?

level: middleimportance: should knowfreq 30%
basics
~10 s

Into OUT_DIR, a per-package directory Cargo passes to the script as an environment variable; the crate then pastes the file in with include!(concat!(env!("OUT_DIR"), "/crc_table.rs")). Writing into src/ instead breaks cargo package verification.

open as a page

A Cargo crate builds its bundled C compression code with the cc crate; why does editing csrc/tinyz.c not rebuild it, and how do you fix that?

level: seniorimportance: should knowfreq 22%
basics
~20 s

cc prints cargo:rerun-if-env-changed lines for CC, CFLAGS and similar variables, and any rerun-if line switches off Cargo's whole-package scan. cc does not list the C sources, so the build script must print cargo::rerun-if-changed for each C file and header.

open as a page

A Cargo build script enables a NEON code path with cargo::rustc-cfg; why must it read CARGO_CFG_TARGET_ARCH instead of cfg!, and what else must it print?

level: seniorimportance: should knowfreq 20%
basics
~20 s

A build script is compiled for and runs on the host, so cfg! describes the host, not the target being built; CARGO_CFG_TARGET_ARCH describes the target. Since Cargo 1.80 it must also print cargo::rustc-check-cfg=cfg(tinyz_neon) on every run, or rustc warns.

open as a page

In a Rust service built with Cargo, what goes in `.cargo/config.toml` so that `cargo build` and `cargo test` target aarch64 Linux from a laptop?

level: middleimportance: must knowfreq 35%
basics
~10 s

Set [build] target to aarch64-unknown-linux-gnu, then add a [target.aarch64-unknown-linux-gnu] table whose linker names a cross linker such as aarch64-linux-gnu-gcc and whose runner names an emulator, which cargo run, test and bench use.

open as a page

In Rust, why does `cargo build --target aarch64-unknown-linux-gnu` fail with E0463 on a fresh laptop, and what fixes it?

level: juniorimportance: should knowfreq 30%
basics
~20 s

Every Rust build links against a precompiled standard library for its target triple, and rustup installs only the host's copy by default. Running rustup target add aarch64-unknown-linux-gnu installs the missing one; a linker for that platform is a separate step.

open as a page

In Cargo, where is `.cargo/config.toml` looked up, and which value wins when several config files and environment variables set the same key?

level: middleimportance: should knowfreq 28%
basics
~10 s

Cargo reads .cargo/config.toml in the current directory and every parent, then $CARGO_HOME/config.toml. A deeper file's strings, numbers and booleans win, arrays are joined, CARGO_ environment variables beat the files, and --config beats both.

open as a page

Why do a Cargo project's `[target.aarch64-unknown-linux-gnu] rustflags` vanish in a CI job that exports `RUSTFLAGS="-D warnings"`, and how do you fix it?

level: seniorimportance: should knowfreq 22%
basics
~10 s

Cargo takes extra rustc flags from one source only: CARGO_ENCODED_RUSTFLAGS, else RUSTFLAGS, else matching [target] rustflags, else build.rustflags. RUSTFLAGS in CI replaces the config's flags; deny warnings through build.warnings instead.

open as a page

How do you make a Cargo build succeed on an air-gapped CI runner that cannot reach crates.io, and what keeps that build reproducible?

level: seniorimportance: should knowfreq 20%
basics
~20 s

Run cargo vendor on a connected machine, commit the vendor directory and the [source.crates-io] replace-with = "vendored-sources" config it prints, then build in CI with --frozen; Cargo.lock and the vendored checksums pin exactly what compiles.

open as a page

In Cargo, what does `cargo yank` do to a broken 2.0.3 release, and which builds keep using it?

level: middleimportance: must knowfreq 38%
basics
~20 s

cargo yank marks 2.0.3 as yanked in the registry index: new resolution never picks it, but nothing is deleted. Any build whose Cargo.lock already names 2.0.3 keeps downloading and compiling it until that lockfile is updated.

open as a page

With `cargo publish`, what happens when you upload version 2.1.0 of a crate, and why can't you fix a mistake by re-uploading 2.1.0?

level: juniorimportance: should knowfreq 35%
basics
~20 s

cargo publish packs the crate into a .crate file, builds that tarball to verify it, and uploads it. A published version is permanent: it cannot be overwritten or deleted, so a fix ships as a new version such as 2.1.1.

open as a page

Before publishing a Rust crate, what does `cargo package` check, and how can an `exclude` entry make its verify build fail?

level: middleimportance: should knowfreq 28%
basics
~20 s

cargo package checks the manifest, builds a .crate from the files include/exclude select, then extracts and compiles that tarball. If exclude drops a file the code reads at compile time, such as an include_str! fixture, the verify build fails.

open as a page

For a team's private Cargo registry, how do you depend on it, publish to it, authenticate CI, and keep internal crates off crates.io?

level: seniorimportance: should knowfreq 25%
basics
~20 s

Name the registry under [registries] in .cargo/config.toml, mark its dependencies registry = "internal", and set publish = ["internal"] to keep crates off crates.io. Tokens come from cargo login --registry internal or CARGO_REGISTRIES_INTERNAL_TOKEN, read by a credential provider.

open as a page

Before publishing 2.1.0 of a Rust crate, what does cargo-semver-checks compare, how does it pick a baseline, and what can it miss?

level: seniorimportance: nice to knowfreq 18%
basics
~20 s

cargo-semver-checks compares rustdoc JSON of the current crate against a baseline release, by default the newest non-yanked, non-prerelease version on crates.io not above yours. It fails if 2.1.0 needs a bigger bump; some breaks, like changed field types, still slip through.

open as a page

How does Cargo run a third-party subcommand such as `cargo audit`, and where does it look for the binary?

level: juniorimportance: should knowfreq 30%
basics
~20 s

When cargo NAME is not a built-in command or an alias, Cargo runs an executable called cargo-NAME, looking in $CARGO_HOME/bin first and then the PATH directories. cargo install puts binaries in ~/.cargo/bin, which is why installed tools just work.

open as a page

What does `cargo audit` check in a Rust project, and which of its findings fail the command by default?

level: middleimportance: should knowfreq 35%
basics
~10 s

cargo audit matches every package version in Cargo.lock against the RustSec advisory database. By default only a vulnerability advisory makes it exit 1; unmaintained, unsound and yanked findings are warnings unless you pass --deny.

open as a page

A payments team's first `cargo deny check` rejects an `MPL-2.0` crate and fails on an unmaintained advisory that `cargo audit` only warned about; why, and how do you tune deny.toml?

level: seniorimportance: should knowfreq 28%
basics
~20 s

cargo-deny's licenses check rejects any license not in [licenses] allow, and its advisories check errors on unmaintained advisories for every crate by default, where cargo-audit only warns. Add a per-crate license exception and a reasoned ignore, or narrow unmaintained.

open as a page

After a Rust workspace's CI moves from `cargo test` to `cargo nextest run`, doctests vanish and one test starts failing; why, and how do you set the job up?

level: seniorimportance: should knowfreq 30%
basics
~20 s

cargo-nextest runs every test in its own process, so a test relying on state another test left behind now fails, and it skips doctests. Remove the hidden dependency and add cargo test --doc as a separate step.

open as a page

For a payments team's Rust CI, which of cargo-audit, cargo-deny, cargo-nextest, cargo-outdated and cargo-machete should block a merge, and which only report?

level: principalimportance: should knowfreq 25%
basics
~20 s

Block on checks a pull request can cause and fix: tests (nextest plus cargo test --doc), cargo-deny licenses, bans and sources, and machete. Gate advisories on lockfile changes and run them on a schedule; keep cargo-outdated a report.

open as a page