Run examples in subfolders using cargo



I have around forty examples in my crate. I want to divide them into four to five folders. Is there a way to run them using cargo, with out putting them in toml file. like

cargo run --example physics/rigid


It’s barely documented, but

You can make




If you want to, say, put five examples in examples/rigid, then you’ll need to modify Cargo.toml; there’s no convention around this specifically.


Thanks for the reply. I haven’t got it, I will put my problem clearly.

Say I have files like this

dinesh@Personal  dem-rs/examples $  
|  Mac => ls

To run the examples I can do

dinesh@Personal  dem-rs/examples $  
|  Mac => cargo run --example rigid_4

If I reorganise and put them into folders, i.e.,

dinesh@Personal  dem-rs/examples $  
|  Mac => ls
rigid     sample    bouncing

Can I run it by

dinesh@Personal  dem-rs/examples $  
|  Mac => cargo run --example rigid/rigid_1

From the answer I understood that it is not possible. Is that right?


You can manually specify examples in your Cargo.toml, unfortunately they need to be valid Rust identifiers so you can’t get the namespacing, but it should be possible to have something like

name = "rigid_1"
path = "examples/rigid/"

name = "rigid_2"
path = "examples/rigid/"


and run that with

cargo run --example rigid_1