-1
├── Cargo.lock
├── Cargo.toml
├── src
│    └── model.rs
└── examples
    └── client
           └──mod.rs

I want to use a struct called Client that exists in examples > client > mod.rs in my model.rs file. My package name is CratesTest in the Cargo.toml.

I tried this in my model.rs:

extern crate CratesTest;

fn main() {
    CratesTest::Client::new(/*snip*/)
}

I get the error:

error[E0433]: failed to resolve: could not find `Client` in `CratesTest`
let client = CratesTest::Client::new(...
                         ^^^^^^ could not find `Client` in `CratesTest`            

I also tried using mod client; but it doesn't bring it into scope.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
theBigCheese88
  • 442
  • 4
  • 16
  • 2
    I'd say there's an inversion of dependency here: it's reasonable for your examples to depend on your library, but why would your library depends on the examples? – Jmb Nov 06 '19 at 10:31
  • 2
    From [the documentation](https://doc.rust-lang.org/cargo/reference/manifest.html#examples): "Files located under examples are example **uses** of the functionality provided by the library" (emphasy mine). Examples use the library, not the reverse. – Denys Séguret Nov 06 '19 at 10:51
  • @Jmb I don't think there's another answer that what you said. You should transmute your comment into an answer – Denys Séguret Nov 06 '19 at 10:54

1 Answers1

2

I'd say there's an inversion of dependency here: it's reasonable for your examples to depend on your library, but why would your library depend on the examples? As pointed out by Denys Séguret, the documentation states:

Files located under examples are example uses of the functionality provided by the library

so examples use the library, not the reverse.

Jmb
  • 18,893
  • 2
  • 28
  • 55
  • See also [How can I use a module from outside the src folder in a binary project, such as for integration tests or benchmarks?](https://stackoverflow.com/a/44002428/155423); [How to import a module from a directory outside of the `src` directory?](https://stackoverflow.com/a/58715429/155423). – Shepmaster Nov 06 '19 at 13:21