Cargo & Tooling Questions with Answers

16 practice questions on Cargo & Tooling (Rust) with answers and step-by-step solutions. Try 10 samples below, then practise all 16 free in the AptiNest app.

Sample questions

Question 1Easy

Where are Rust unit tests usually written?

  1. Only in Python
  2. In a #[cfg(test)] module with #[test] functions, run by cargo test
  3. In the Cargo.lock file
  4. In a separate tests.toml file

Answer: B. In a #[cfg(test)] module with #[test] functions, run by cargo test

Explanation

Integration tests go in a top-level tests/ directory.

Step-by-step solution

  1. Unit tests usually sit in a cfg(test) module inside the source file.
  2. Functions marked with the test attribute are run by cargo test.
  3. So option B is the correct answer.

Why the other options are wrong

  • A. Rust tests are written in Rust, not Python.
  • C. Cargo.lock records versions; it does not hold tests.
  • D. Rust unit tests do not live in a tests.toml file.

Tip: Put unit tests in a cfg(test) module; integration tests go in the tests directory.

Question 2Easy

Which attribute marks a function as a unit test run by cargo test?

  1. #[unit]
  2. #[check]
  3. #[cfg(run)]
  4. #[test]

Answer: D. #[test]

Explanation

Functions marked #[test] are collected and executed by cargo test; a panic means failure.

Step-by-step solution

  1. Test harness looks for #[test].
  2. Panic equals fail.
  3. No panic equals pass.

Why the other options are wrong

  • A. There is no #[unit] attribute.
  • B. #[check] does not exist.
  • C. cfg(run) is not a thing.

Question 3Easy

What does cargo check do?

  1. Type-checks the code without producing a final binary
  2. Runs all the tests in the crate
  3. Formats the source code to the standard style
  4. Publishes the crate to the public registry

Answer: A. Type-checks the code without producing a final binary

Explanation

cargo check runs the compiler up to type and borrow checking, which is faster than a full build.

Step-by-step solution

  1. It stops before code generation.
  2. No executable is made.
  3. Hence faster feedback.

Why the other options are wrong

  • B. That is cargo test.
  • C. That is cargo fmt.
  • D. That is cargo publish.

Question 4Easy

Which command creates a new binary project named demo?

  1. cargo init --lib demo.rs
  2. rustc new demo
  3. cargo create demo
  4. cargo new demo

Answer: D. cargo new demo

Explanation

cargo new demo makes a folder with Cargo.toml and src/main.rs.

Step-by-step solution

  1. Cargo manages projects.
  2. new creates a folder.
  3. A binary is the default.

Why the other options are wrong

  • A. That is not how you start a binary project.
  • B. rustc compiles; it does not create projects.
  • C. cargo create does not exist.

Question 5Medium

What does cargo fmt do?

  1. Generates a Cargo.toml
  2. Finds lint warnings
  3. Formats code with rustfmt in the standard style
  4. Checks for vulnerable dependencies

Answer: C. Formats code with rustfmt in the standard style

Explanation

Consistent formatting keeps diffs clean; clippy handles lints.

Step-by-step solution

  1. Cargo fmt runs rustfmt on the project.
  2. Rustfmt formats code in the standard Rust style.
  3. So option C is the correct answer.

Why the other options are wrong

  • A. Cargo fmt does not generate Cargo.toml.
  • B. Lint warnings come from cargo clippy.
  • D. Vulnerable dependencies are checked by a separate audit tool.

Tip: Run cargo fmt before a commit to keep diffs clean.

Question 6Medium

cargo build vs cargo build --release:

  1. The default build is fully optimised, and --release additionally removes all bounds checks
  2. They are identical
  3. --release adds debug symbols only
  4. --release enables optimisations; the default is a faster-compiling debug build

Answer: D. --release enables optimisations; the default is a faster-compiling debug build

Explanation

Debug builds also enable integer overflow checks that panic.

Step-by-step solution

  1. The default cargo build uses the debug profile, which compiles faster.
  2. Cargo build --release turns on optimisations, which take longer to compile.
  3. So option D is the correct answer.

Why the other options are wrong

  • A. The default build is not fully optimised; the release build is.
  • B. The two builds differ in optimisation and compile speed.
  • C. --release is about optimisation, not only debug symbols.

Tip: Use cargo build --release when you measure speed or ship the program.

Question 7Medium

What does cargo clippy do?

  1. Downloads the standard library source for offline use
  2. Publishes crates
  3. Runs lints that catch common mistakes and unidiomatic code
  4. Formats code

Answer: C. Runs lints that catch common mistakes and unidiomatic code

Explanation

Clippy suggests improvements beyond compiler warnings; cargo fmt handles formatting.

Step-by-step solution

  1. Clippy is a set of lints that check Rust code.
  2. It catches common mistakes and unidiomatic code beyond the compiler warnings.
  3. So option C is the correct answer.

Why the other options are wrong

  • A. Clippy does not download the standard library source.
  • B. Publishing is done with cargo publish.
  • D. Formatting is done by cargo fmt.

Tip: Run cargo clippy for lints and cargo fmt for formatting.

Question 8Medium

What does the [dev-dependencies] section of Cargo.toml hold?

  1. Crates needed only for tests, examples and benchmarks
  2. Crates only used on the developer's OS
  3. Crates compiled in debug mode only
  4. Optional crates for the release build

Answer: A. Crates needed only for tests, examples and benchmarks

Explanation

Dev-dependencies are not part of the published library or final binary; they are built only for tests and similar targets.

Step-by-step solution

  1. Some crates are for testing only.
  2. They go under dev-dependencies.
  3. Users of your crate don't pull them.

Why the other options are wrong

  • B. It is not OS-specific.
  • C. Profile choice is separate.
  • D. Optional features use the optional key.

Question 9Medium

In Rust, a crate is:

  1. A single function
  2. A thread
  3. A compilation unit: a library or binary
  4. A type of smart pointer

Answer: C. A compilation unit: a library or binary

Explanation

Crates are published to crates.io; modules organise code within a crate.

Step-by-step solution

  1. A crate is the unit of compilation in Rust.
  2. It is either a library or a binary.
  3. So option C is the correct answer.

Why the other options are wrong

  • A. A crate holds many functions; it is not a single function.
  • B. A crate is not a thread.
  • D. A crate is not a smart pointer.

Tip: Modules organise code inside a crate; crates are what get compiled and published.

Question 10Hard

What does the unsafe keyword allow?

  1. Turning off the borrow checker and type checker for the whole crate and all its dependencies
  2. Running code without any memory allocation
  3. Ignoring all type errors
  4. Operations the compiler cannot verify, like dereferencing raw pointers

Answer: D. Operations the compiler cannot verify, like dereferencing raw pointers

Explanation

unsafe lets you take responsibility for specific invariants; borrow checking still applies to references.

Step-by-step solution

  1. unsafe unlocks a few extra operations.
  2. Examples: dereferencing raw pointers, calling unsafe functions.
  3. The borrow checker still applies elsewhere.

Why the other options are wrong

  • A. It does not disable checks for the whole crate.
  • B. It does not affect allocation.
  • C. Type errors are still errors.

Practise all 16 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Rust topics · All topics