Sample questions
Question 1Easy
Where are Rust unit tests usually written?
- Only in Python
- In a #[cfg(test)] module with #[test] functions, run by cargo test
- In the Cargo.lock file
- 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
- Unit tests usually sit in a cfg(test) module inside the source file.
- Functions marked with the test attribute are run by cargo test.
- 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?
- #[unit]
- #[check]
- #[cfg(run)]
- #[test]
Answer: D. #[test]
Explanation
Functions marked #[test] are collected and executed by cargo test; a panic means failure.
Step-by-step solution
- Test harness looks for #[test].
- Panic equals fail.
- 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?
- Type-checks the code without producing a final binary
- Runs all the tests in the crate
- Formats the source code to the standard style
- 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
- It stops before code generation.
- No executable is made.
- 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?
- cargo init --lib demo.rs
- rustc new demo
- cargo create demo
- 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
- Cargo manages projects.
new creates a folder.- 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
- Generates a Cargo.toml
- Finds lint warnings
- Formats code with rustfmt in the standard style
- 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
- Cargo fmt runs rustfmt on the project.
- Rustfmt formats code in the standard Rust style.
- 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:
- The default build is fully optimised, and --release additionally removes all bounds checks
- They are identical
- --release adds debug symbols only
- --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
- The default cargo build uses the debug profile, which compiles faster.
- Cargo build --release turns on optimisations, which take longer to compile.
- 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?
- Downloads the standard library source for offline use
- Publishes crates
- Runs lints that catch common mistakes and unidiomatic code
- 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
- Clippy is a set of lints that check Rust code.
- It catches common mistakes and unidiomatic code beyond the compiler warnings.
- 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?
- Crates needed only for tests, examples and benchmarks
- Crates only used on the developer's OS
- Crates compiled in debug mode only
- 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
- Some crates are for testing only.
- They go under dev-dependencies.
- 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
- A single function
- A thread
- A compilation unit: a library or binary
- 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
- A crate is the unit of compilation in Rust.
- It is either a library or a binary.
- 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?
- Turning off the borrow checker and type checker for the whole crate and all its dependencies
- Running code without any memory allocation
- Ignoring all type errors
- 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
- unsafe unlocks a few extra operations.
- Examples: dereferencing raw pointers, calling unsafe functions.
- 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 →