Explore core Rust testing and debugging concepts with this quiz designed to reinforce best practices, troubleshoot errors, and improve test reliability. Ideal for those eager to strengthen their foundational knowledge in Rust unit testing, assertions, and basic debugging methods.
Which attribute must you add above a function to mark it as a test in Rust?
Explanation: The correct way to designate a test function in Rust is by placing the #[test] attribute above it. '#test' is incorrect due to syntax. 'test()' is not an attribute, but a function call which is invalid in this context. '[test]' is missing the necessary hash and square brackets, so it will not be recognized.
Which macro should you use to verify that two values are equal in a Rust test?
Explanation: 'assert_eq!' checks that two values are equal and is specifically designed for comparisons. 'assert_false!' does not exist in Rust and would not work. 'assert_same!' is not a standard Rust macro. 'equals_assert!' is also incorrect and unrecognized by Rust. The only valid choice for equality is 'assert_eq!'.
What is the default command to run all tests in a Rust project using Cargo?
Explanation: 'cargo test' runs all tests in the project by default. 'cargo build --test' only compiles the tests without running them. 'cargo run tests' is a syntactically incorrect command. 'cargo check test' checks syntax but does not execute tests.
Which attribute should be used to indicate that a Rust test should pass only if it causes a panic?
Explanation: The attribute #[should_panic] tells Rust that the test is expected to panic, and it will pass only if a panic occurs. '#[should_fail]' is not a valid attribute. '#[panic]' and '#[test_panic]' are both incorrect and unrecognized by the test framework.
How can you temporarily disable a test in Rust so that it is not run by default?
Explanation: Adding #[ignore] above a test function will skip it by default when 'cargo test' is run. '#[skip]' and '#ignore' are incorrect due to invalid attribute formatting. '#ignored' is also not recognized by the compiler for skipping tests.
Which macro is commonly used in Rust for printing debug output to the console during test execution?
Explanation: 'println!' is the standard macro for printing to the console, and it works inside tests for debug purposes. 'debug_println!' is not a recognized macro. 'log!' does not exist as a macro in standard Rust. 'showout!' is not valid and will cause a compilation error.
From which location can you directly test private functions in a Rust module?
Explanation: Private functions are accessible for testing within the same module or submodule due to Rust's module privacy rules. External crates and sibling modules cannot access private functions directly. 'From anywhere in the workspace' is wrong because privacy rules prevent it.
If a Rust test function completes without panic, what does it indicate?
Explanation: In Rust, a test passes if it returns without a panic, typically meaning no assertions failed. If it is ignored, it would not run, making 'The function has been ignored' incorrect. Compilation errors prevent execution entirely. A test failing due to an assertion would cause a panic, so it's not the correct answer here.
Which assertion macro allows custom messages using debug formatting for failed tests in Rust?
Explanation: 'assert_eq!' allows custom messages and prints debug representations of values when assertions fail. 'assert_eq_debug!' is not a standard macro. 'assert!' supports custom messages but is not specific to debug formatting of two values. 'debug_assert!' is meant for debug builds and not for explicitly adding messages with debug output.
Which standard tool can be used to debug Rust programs by inspecting variables and stepping through code line by line?
Explanation: 'gdb' is a widely-used debugger that allows inspection and interactive stepping through Rust binaries. 'Cargo Clippy' analyzes code for linting but is not a debugger. 'rustup' is used for managing Rust toolchains, not for debugging. 'depcheck' is unrelated to Rust debugging and is not a recognized tool in this context.