Skip to content

Make it easy to list and run all examples #8356

Open
@robinmoussu

Description

@robinmoussu

Describe the problem you are trying to solve

Some project have a list of example, usually in the example folder. It would be nice if it was possible to:

  1. list them all (so you can run them later with cargo run --example $name_of_the_example)
  2. run all of them in one command (it may be useful in CI).
    • a bit like cargo test, this should count the number of panic/non 0 exit code
    • it should be possible to provide a list of arguments that would be passed to each the examples

Describe the solution you'd like

  1. I think a new command cargo list that would output (in separate sections) the list of examples, the list of binaries, and the list of libraries provided by the current crate would be a nice addition. It should be possible to filter the output with flags like --example, --binaries or --libraries. Maybe the list of features available could also be listed?

  2. I think a new subcommand of cargo run should be added: cargo run --examples. Like cargo run, all arguments after -- should be passed to the examples binaries. It would be equivalent to do:

for example in examples/*.rs
do
    cargo run --example "$(basename "$example")" -- $args
done

A flag --exclude $example may be added to run all examples but $example (and would only make sense if --examples is used). Similarly, cargo run --binaries could be added to run all binaries of a crate.

Notes

In some cases, like a 3d crate, or interactive example, it would not be wise to run all examples but I don't think it's an issue. The proposed cargo list and cargo run --examples would still be really useful for most cases.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-cargo-targetsArea: selection and definition of targets (lib, bins, examples, tests, benches)A-cliArea: Command-line interface, option parsing, etc.A-examplesArea: example targetsC-feature-requestCategory: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`S-triageStatus: This issue is waiting on initial triage.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions