Can an example have its own library?

Cargo question: Can an executable example have its own library?
Examples don't have their own Cargo.toml file, unlike targets. So there's no obvious way to express this in Cargo.toml form.

Use case: I'm working on a GUI library. The library itself has the general-purpose part. The example uses it, and the example also needs a library of all the dialogs specific to the example. This separates the UI from the parts that do the work, so people can work on the menus separately from the rest of the system.

Some projects use a workspace to hold multiple crates, for example the ash project (a Vulkan implementation in Rust). The example is a crate itself, not an "examples" folder in src.

Could you move the dialogs into their own module inside the examples/ folder?

For example, this file structure works:

$ tree .
├── Cargo.lock
├── Cargo.toml
├── examples
│   ├── dialogs
│   │   ├──
│   │   └──
│   └──
└── src

3 directories, 6 files

Where looks like this:

mod dialogs;

fn main() {
    let path = dialogs::open_file_dialog();
    println!("Reading data from {path:?}");

Otherwise if the dialogs come from a different crate and it's just a matter of pulling them in, you can add it as a dev-dependency.

That's pretty much what I did. In the real application, they become a library. This is just for my ui-mock demo of my libui library. For a real application, the examples folder is not used, and you have to provide a dialogs folder of your own.

Yes, use dev-dependencies.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.