Rust Testing and Debugging Essentials Quiz Quiz

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.

  1. Test Functions Setup

    Which attribute must you add above a function to mark it as a test in Rust?

    1. [test]
    2. #test
    3. test()
    4. #[test]

    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.

  2. Assertion Macros

    Which macro should you use to verify that two values are equal in a Rust test?

    1. assert_same!
    2. assert_eq!
    3. equals_assert!
    4. assert_false!

    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!'.

  3. Running Tests

    What is the default command to run all tests in a Rust project using Cargo?

    1. cargo run tests
    2. cargo test
    3. cargo check test
    4. cargo build --test

    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.

  4. Handling Panics in Tests

    Which attribute should be used to indicate that a Rust test should pass only if it causes a panic?

    1. #[test_panic]
    2. #[panic]
    3. #[should_fail]
    4. #[should_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.

  5. Ignoring Tests Temporarily

    How can you temporarily disable a test in Rust so that it is not run by default?

    1. #ignore
    2. #[skip]
    3. #[ignore]
    4. #ignored

    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.

  6. Printing Debug Information

    Which macro is commonly used in Rust for printing debug output to the console during test execution?

    1. debug_println!
    2. println!
    3. showout!
    4. log!

    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.

  7. Testing Private Functions

    From which location can you directly test private functions in a Rust module?

    1. From a sibling module
    2. Only inside the same module or submodule
    3. Any external crate
    4. From anywhere in the workspace

    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.

  8. Meaning of a Passing Test

    If a Rust test function completes without panic, what does it indicate?

    1. The function has been ignored
    2. The test failed due to an assertion
    3. The test has passed successfully
    4. There was a compilation error

    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.

  9. Using Debug Formatting in Assertions

    Which assertion macro allows custom messages using debug formatting for failed tests in Rust?

    1. assert_eq_debug!
    2. debug_assert!
    3. assert_eq!
    4. assert!

    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.

  10. Main Tool for Debugging

    Which standard tool can be used to debug Rust programs by inspecting variables and stepping through code line by line?

    1. gdb
    2. rustup
    3. depcheck
    4. Cargo Clippy

    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.