[Solved] Using examples crate lib in another examples crate

Hi all,

I am trying to create a lib crate in cargo's examples directory so that I can use it in another crate in the examples directory. I have read Cargo's documentation on this but I am still unable to get it to work.

I am getting the following error:

$ cargo +nightly build --examples
   Compiling example_crate v0.1.0 (.../example_crate)
error[E0432]: unresolved import `b`
 --> examples/c.rs:1:5
  |tr
1 | use b::b;
  |     ^ Could not find `b` in `{{root}}`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0432`.
error: Could not compile `example_crate`.

To learn more, run the command again with --verbose.

When I look at the target directory I do see that the lib is created but it is not included in the deps directory:

$ tree target/debug/examples/
target/debug/examples/
├ b-1dcc503f23e0b76b.d
├ b-7b617d848b897e49.d
├ b-e7c00f0c42ff9607.d
├ c-4cad30ab9241529b.d
├ c-ecabb81ac2bff21c.d
├ libb-1dcc503f23e0b76b.rmeta
├ libb-7b617d848b897e49.rlib
├ libb.d
├ libb-e7c00f0c42ff9607.rmeta
├ libb.rlib
└ libb.rmeta

0 directories, 11 files
tree target/debug/deps/
target/debug/deps/
├── example_crate-08f3e93bc1a6d201.d
├── example_crate-1a178177b8db9299.d
├── example_crate-377ea239c50fee8d.d
├── example_crate-472d54d512ae0a93.d
├── example_crate-b9a91e87870b8d8f
├── example_crate-b9a91e87870b8d8f.d
├── libexample_crate-08f3e93bc1a6d201.rlib
├── libexample_crate-1a178177b8db9299.rmeta
├── libexample_crate-377ea239c50fee8d.rmeta
└── libexample_crate-472d54d512ae0a93.rmeta

I have uploaded a git repo with all of the details here. Any help is greatly appreciated.

Thanks!

Replacing c.rs with

mod b;

use self::b::b;
use example_crate::a;

fn c() {}

fn main() {
    a();
    b();
    c();
}

makes this compile fine with cargo build --examples.

Thanks a lot @matt1992, this does unblock me. I think the project is now 1) creating a lib crate for b that is not being used and 2) including the b module in the c crate. My actual project has crate level features. Once I add those to this example, I get the following warnings:

 $ cargo +nightly build --examples
   Compiling example_crate v0.1.0 (.../example_crate)
warning: unused attribute
 --> examples/b.rs:1:1
  |
1 | #![feature(async_await, futures_api)]
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: #[warn(unused_attributes)] on by default

warning: crate-level attribute should be in the root module
 --> examples/b.rs:1:1
  |
1 | #![feature(async_await, futures_api)]
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    Finished dev [unoptimized + debuginfo] target(s) in 0.40s

My question now is how do I configure cargo to either:

  1. Ignore b when it is building the examples crates so that I can import b as a module on the other examples crates.
  2. Build b as a crate and import it as a crate on the other examples crates?

The new example code with crate level features can be found here.

Thanks!

Cargo doesn't support including example extern crates. Removing it from the [[example]] and importing as a mod b works just fine. This was documented in this cargo issue.