2

When creating a project with a test like so:

cargo init --bin projectname
mkdir projectname/tests
echo "extern crate projectname;" > projectname/tests/test.rs
cd projectname/
cargo build

I get this error when testing:

cargo test
   Compiling projectname v0.1.0 (file:///home/username/Lab/projectname)
error[E0463]: can't find crate for `projectname`
 --> tests/test.rs:1:1
  |
1 | extern crate projectname;
  | ^^^^^^^^^^^^^^^^^^^^^^^^^ can't find crate

How can I access the functions in ´projectname/src/main.rs´ from projectname/tests/test.rs?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366

1 Answers1

4

How can I access functions in ´projectname/src/main.rs´ from projectname/tests/test.rs?

You cannot.


A binary cannot be used a an external crate (the same way as you can't use a ELF binary as a shared object/library)

You just have to change your initialisation to

cargo init --lib projectname

or rename your main.rs to lib.rs

If you really want to stick with a main, you may look at Rust package with both a library and a binary?.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
hellow
  • 12,430
  • 7
  • 56
  • 79