How do I run binaries in the example folder?

I was looking at the example folder of ncollide2d, and I would like to run those binaries. How do I do it? They don't seems to be ran with cargo test, and cargo run --bin aabb2d doesn't work (there is a file aabb2d.rs with a main in the example directory). cargo help wasn't really helpful either.

cargo run --example aabb2d should work.

1 Like

Thanks. I somehow didn't saw it.

Btw, is there a way to build and run all examples?

cargo build --examples will build most examples, but skips examples dependent on features. I think cargo build --examples --all-features should build all of them.

As for running, I don't know of any cargo command to do that. If this is for CI, it might be best to just revert to a bash script or something similar? Something like this could work, if extra scripting is OK:

for file in examples/*.rs; do
    cargo run --example "$(basename -s .rs $file)"
done
1 Like

Thanks.

It it was anything but Rust, I would have not even asked (since this shell script is definitively straightforward), but it feels so strange to not have it already available with a cargo command. Even code in doc comment is run with cargo test.

1 Like

I think it could definitely be useful! The only reason I think it might not be included is many examples are written to be more substantive than test code? Like, for gui crates I don't want to automatically run a bunch of real applications, or if there's a CLI tool it would probably require arguments for each example.

A command to run all of them with no arguments could be useful in some cases! But there are enough cases where it wouldn't be as useful that if it were added, I'd want more. I guess it doesn't really feel like it's as simple, in general, as cargo test or doctests?

It's the same thing with main crate binaries - you can build all of them, but not run all of them automatically.

2 Likes

I submitted the idea.

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.