Why do I get duplicative warnings after running `cargo test <single test>`

main.rs

lib.rs

When I run cargo test one_result, I see the following warning:

Full Log
❯ cargo test one_result
warning: unused variable: `query`
  --> src/lib.rs:31:19
   |
31 | pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
   |                   ^^^^^ help: if this is intentional, prefix it with an underscore: `_query`
   |
   = note: `#[warn(unused_variables)]` on by default

warning: unused variable: `contents`
  --> src/lib.rs:31:32
   |
31 | pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
   |                                ^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_contents`

warning: 2 warnings emitted

warning: unused variable: `query`
  --> src/lib.rs:31:19
   |
31 | pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
   |                   ^^^^^ help: if this is intentional, prefix it with an underscore: `_query`
   |
   = note: `#[warn(unused_variables)]` on by default

warning: unused variable: `contents`
  --> src/lib.rs:31:32
   |
31 | pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
   |                                ^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_contents`

warning: 2 warnings emitted

    Finished test [unoptimized + debuginfo] target(s) in 0.00s
     Running unittests (target/debug/deps/minigrep-b6590e6e31b9e448)

running 1 test
test test::one_result ... FAILED

failures:

---- test::one_result stdout ----
thread 'test::one_result' panicked at 'assertion failed: `(left == right)`
  left: `["safe, fast, productive."]`,
 right: `[]`', src/lib.rs:76:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace


failures:
    test::one_result

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 3 filtered out; finished in 0.00s

error: test failed, to rerun pass '--lib'

Why is that?

cargo test compiles lib.rs twice: Once in test mode, which produces an executable program that runs the library’s unit tests, and once as a regular library for use in other test executables (including integration tests, unit tests from main.rs, and doctests).

You can pass the --lib flag to tell Cargo to only build and run the library unit tests:

cargo test --lib one_result
3 Likes

In Rust 1.55 these duplicate warnings will be hidden thanks to this PR:
https://github.com/rust-lang/cargo/pull/9675

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.