Accessing crate from an example

I'd like to make use of the current crate from an example therein. According to all docs I could find (searching for "example" is meh, btw), this should be straight forward, but I seem to have missed something trivial.

src/main.rs (crate name xxx)

pub fn test() {
    println!("remote test");
}

fn main() {
    println!("Hello, world!");
}

examples/ex.rs

use xxx::test;

pub fn main() {
    test();
    println!("test");
}

cargo run --example ex

error[E0432]: unresolved import `xxx`
 --> examples/ex.rs:1:5
  |
1 | use xxx::test;
  |     ^^^ use of undeclared type or module `xxx`

How can I make the crate visible/addressable to the example code?

Main.rs produces a binary, not a library. Since it’s not a library, it’s not available for you to use in other crates in your package (like the example)

1 Like

Thanks a lot for the quick response (I just found it out in the very same second).
I might transform my code into a lib/bin hybrid or split it into several crates.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.