Examples in a sub-directory of examples

Is it possible to run example programs, via cargo, in a sub-directory of examples like:


The Cargo book shows an example like this:

├── examples/
│   ├── simple.rs
│   └── multi-file-example/
│       ├── main.rs
│       └── ex_module.rs

However, I cannot get the files in the sub-directory to build via cargo build --examples or cargo run --example.

cargo is looking for a file with the exact name of main.rs. You can probably just rename bar.rs to main.rs in your example and run it with cargo r --example multi-file-example.

Btw, this requirement is described in the docs link you posted - it's in the paragraph under the bullet points. It also applies to the src/bin, benches, and tests directories, so you can have multi-file directories in those places too.

Yes, that is correct. I get it now. Thanks.

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.