Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upRFC: Project-based Examples for Cargo Projects #2517
Conversation
luojia65
added some commits
Aug 1, 2018
Centril
added
the
T-cargo
label
Aug 6, 2018
killercup
reviewed
Aug 6, 2018
| # Rationale and alternatives | ||
| [rationale-and-alternatives]: #rationale-and-alternatives | ||
|
|
||
| There could be another way to implement project-based examples by introducing `cargo example` subcommand. However by doing this we must change our way to run examples now by `cargo run --example <NAME>` which is already widely accepted by rust community. |
This comment has been minimized.
This comment has been minimized.
killercup
Aug 6, 2018
•
Member
Sorry, I'll be devil's advocate here for a bit.
Another (valid but boring) alternative: Change nothing but actively tell people not to use cargo run --example in some cases. You can use cargo's workspace feature to use a common target directly (faster compile times), and just run cargo commands in the subdirectory. This is what diesel and quicli do. This also allows you to have multiple binaries in your example projects, as well as using your shell prompt's path printing feature as an indicator for the current example project context.
(I'm pretty sure this was already mentioned somewhere but I feel like it should also be listed in this section.)
luojia65
added some commits
Aug 6, 2018
ehuss
reviewed
Aug 6, 2018
|
|
||
| There could be another way to implement project-based examples by introducing `cargo example` subcommand. However by doing this we must change our way to run examples now by `cargo run --example <NAME>` which is already widely accepted by rust community. | ||
|
|
||
| It may also be suggested that we could somehow *not* implement this project-based cargo examples, but suggest the developers to build an example by adding all of them as the workspace members, and use the `cargo run -p <NAME>` command to run, test or bench it as it indicates the path of the root project, like what we already have in projects like [diesel] and [quicli]. By this way we share the `target` folder with the root project to improve compile speed as well as type less `cd` commands. However as we should add all our examples one by one into workspace if we do this, once we forget to add one newly-created example and users who does not know `cargo run -p` somehow run it by `cd examples/<NAME>` and `cargo run`, a huge `target` folder in the folder of the example would be created before being noticed. |
This comment has been minimized.
This comment has been minimized.
ehuss
Aug 6, 2018
Contributor
This isn't quite correct. Once you have a workspace, if you forget to add a new sub-package to the members list, it will refuse to compile. cargo new will also yell at you so you don't forget.
killercup
reviewed
Aug 7, 2018
|
Thanks for the update! Random other things/tip for your next RFC: It's much easier to add useful inline comments if you split your paragraphs over multiple lines :) http://rhodesmill.org/brandon/2012/one-sentence-per-line/ |
|
|
||
| It may also be suggested that we could somehow *not* implement this project-based cargo examples, but suggest the developers to build an example by adding all of them as the workspace members, and use the `cargo run -p <NAME>` command to run, test or bench it as it indicates the path of the root project, like what we already have in projects like [diesel] and [quicli]. By this way we share the `target` folder with the root project to improve compile speed as well as type less `cd` commands, but we have to add all our examples one by one as subpackages into the workspace. | ||
|
|
||
| [diesel]: https://github.com/diesel-rs/diesel/blob/master/Cargo.toml#L1-L26 |
This comment has been minimized.
This comment has been minimized.
killercup
Aug 7, 2018
Member
You should always use Github URLs containing the commit hash, otherwise the content will change and the highlighted lines will point to something totally different. You can easy get that by pressing y on any source page on Github.
luojia65
added some commits
Aug 7, 2018
tanriol
reviewed
Aug 9, 2018
|
|
||
| There could be another way to implement project-based examples by introducing `cargo example` subcommand. However by doing this we must change our way to run examples now by `cargo run --example <NAME>` which is already widely accepted by rust community. | ||
|
|
||
| It may also be suggested that we could somehow *not* implement this project-based cargo examples, but suggest the developers to build an example by adding all of them as the workspace members, and use the `cargo run -p <NAME>` command to run, test or bench it as it indicates the path of the root project, like what we already have in projects like [diesel] and [quicli]. By this way we share the `target` folder with the root project to improve compile speed as well as type less `cd` commands, but we have to add all our examples one by one as subpackages into the workspace. |
This comment has been minimized.
This comment has been minimized.
tanriol
Aug 9, 2018
Sub-alternative: automatically add projects in examples/ to the workspace, just like path dependencies are auto-added.
luojia65
added some commits
Aug 10, 2018
This comment has been minimized.
This comment has been minimized.
tanriol
commented
Aug 10, 2018
|
Do you expect the examples to be considered workspace members? If they are, they share the same |
This comment has been minimized.
This comment has been minimized.
ExpHP
commented
Aug 13, 2018
|
Nit: Related thought: It would be interesting if an explicit |
This comment has been minimized.
This comment has been minimized.
chrysn
commented
Aug 14, 2018
|
How would crates.io treat those examples? Right now, with example dependencies in dev-dependencies, I can find examples of how linux-embedded-hal is used easily. How would the proposal affect that? |
pganssle
added a commit
to pganssle/pyo3
that referenced
this pull request
Aug 21, 2018
pganssle
added a commit
to pganssle/pyo3
that referenced
this pull request
Aug 21, 2018
pganssle
added a commit
to pganssle/pyo3
that referenced
this pull request
Aug 21, 2018
pganssle
added a commit
to pganssle/pyo3
that referenced
this pull request
Aug 21, 2018
pganssle
added a commit
to pganssle/pyo3
that referenced
this pull request
Aug 21, 2018
pganssle
added a commit
to pganssle/pyo3
that referenced
this pull request
Aug 21, 2018
luojia65
added some commits
Aug 23, 2018
This comment has been minimized.
This comment has been minimized.
|
Edit: consider |
luojia65 commentedAug 6, 2018
•
edited
This RFC enables cargo to run, test or bench examples which are arranged and stored as a project-based cargo project in
examplesfolder. A project-based example imply a complete cargo project withCargo.toml,srcfolder,main.rs, build script likebuild.rsand maybe other necessary files in it.For example, by using a simple command
cargo new --example abc, you can create an example namedabcfor your project, which has this folder structure after creation:And, as well, you may conveniently run this example using
cargo run --example abc, the same command as what you use now.Rendered
Discussion on Internals
Thanks to @Nemo157, @thomwiggers, @chrysn, @ehuss for suggestions and ideas.