If I have the following:
├── Cargo.lock
├── Cargo.toml
├── main
│ ├── Cargo.toml
│ └── src
│ └── main.rs
├── module2
│ ├── Cargo.toml
│ └── src
│ ├── lib.rs
│ └── builder.rs
Where the Cargo.toml file in the root is the following:
[workspace]
members = [
"main",
]
I want to access a function from builder.rs in main when testing(i.e. cfg(test)), how can I do so?
Module2 is a library(it was created by running cargo new module2 --lib.
I tried the following:
// module2/builder.rs
pub fn build() { /*...*/ }
// module2/lib.rs
#[cfg(test)]
mod mock;
#[cfg(test)]
pub use mock::build;
// main/Cargo.toml
// ...
[dependencies]
module2 = { path = "../module2" }
// main.rs
#[cfg(test)]
use module2::build;
/*
...
*/
This doesn't work and I get the following error:
error[E0432]: unresolved import `module2::build`
--> main/src/main.rs:3:5
|
3 | use module2::build;
| ^^^^^^^^^^^^^^ no `build` in the root
module2is not in your workspace; is this intentional?