0

I have some code in src/main.rs, including pub fn display(word: &str, guesses: &str) -> String { ... } (it runs fine with cargo run).

I have

[package]
name = "hangman"
version = "0.1.0"
edition = "2021"

in Cargo.toml

and I have

use hangman::*;

#[test]
fn test_display() {
    assert_eq!(display("banana", ""), String::from("_ _ _ _ _ _"))
}

in tests/hangman.rs

When I run cargo test I get the following errors:

   Compiling hangman v0.1.0 (/home/feature/projects/hangman)
error[E0432]: unresolved import `hangman`
 --> tests/hangman.rs:2:5
  |
2 | use hangman::*;
  |     ^^^^^^^ use of undeclared crate or module `hangman`

error[E0425]: cannot find function `display` in this scope
 --> tests/hangman.rs:5:16
  |
5 |     assert_eq!(display("banana", ""), String::from("_ _ _ _ _ _"))
  |                ^^^^^^^ not found in this scope

Some errors have detailed explanations: E0425, E0432.
For more information about an error, try `rustc --explain E0425`.
error: could not compile `hangman` due to 2 previous errors

Why is hangman undeclared if it's named as such in Cargo.toml? Is there another place I need to declare it?

feature_engineer
  • 1,088
  • 8
  • 16
  • 1
    You can't, binary crates can't be imported, instead define functions you want to test in a library crate (can be the same directory just in `lib.rs`) and import them from your `main.rs` and your tests. – cafce25 Jan 09 '23 at 11:07

0 Answers0