book icon indicating copy to clipboard operation
book copied to clipboard

RFC: Add example of `cargo add`

Open buggymcbugfix opened this issue 1 year ago • 2 comments

Hi! I'm working through the Rust Book for the first time and I noticed that Cargo supports cargo add now. Maybe worth adding that to the book?

Progress

  • [x] Add example for how to use cargo add to add the rand dependency in chapter 2 ~- [ ] Re-number the no-listings so they appear in chronological order (haven't done this yet to minimise the diff for the RFC phase)~ ~- [ ] Explain what the output means (help, I don't know what the Features part means, it's not explained in the Cargo documentation)~ ~- [ ] Make a mention of the minimal Cargo version that supports this?~
  • [x] Update the Cargo documentation to explain what the Features output means.

buggymcbugfix avatar Aug 28 '22 22:08 buggymcbugfix

Hi! Thank you so much for this! I'm definitely into this, with one caveat: I'm working on a new print version of the book right now that won't include this, so I'm not going to merge this for the online version until I finish that in a few weeks or months.

I'm also very impressed that you've successfully figured out how to work with my convoluted listing management!!!

I actually haven't had a chance to use cargo add yet. It's really important that what ends up in Cargo.toml is rand = "0.8.3". I can't tell for sure, but based on the Cargo.lock in this PR, it looks like cargo add [email protected] ends up putting rand = "=0.8.3" in Cargo.toml? That's too strict for what the text is illustrating-- I do want the reader to get 0.8.5 since that has been released.

Is there a different cargo add syntax that will result in rand = "0.8.3" in Cargo.toml?

Re-number the no-listings so they appear in chronological order (haven't done this yet to minimise the diff for the RFC phase)

I'd prefer this in a separate PR, if you do it at all-- it's not a huge deal for me!

Explain what the output means (help, I don't know what the Features part means, it's not explained in the Cargo documentation)

It looks like it's printing the Cargo features available from the crate just added. Sounds like you could send in a cargo add doc PR too if you want!

Make a mention of the minimal Cargo version that supports this?

The minimal Rust version is mentioned in the title page, so I don't think it's necessary to reiterate here.

carols10cents avatar Aug 29 '22 13:08 carols10cents

It's really important that what ends up in Cargo.toml is rand = "0.8.3". I can't tell for sure, but based on the Cargo.lock in this PR, it looks like cargo add [email protected] ends up putting rand = "=0.8.3" in Cargo.toml? That's too strict for what the text is illustrating-- I do want the reader to get 0.8.5 since that has been released.

My bad, I cargo-culted the lockfile from one of the other no-listings where the lockfile has 0.8.3. I just tried again and I can confirm it does what you want: cargo add [email protected] adds rand = "0.8.3" in the Cargo.toml, which gets resolved to 0.8.5 for me.

I'd prefer this in a separate PR, if you do it at all-- it's not a huge deal for me!

No, I don't mind at all, I thought it might be confusing for others if I don't "defragment" :)

It looks like it's printing the Cargo features available from the crate just added.

Ah, cool, thanks!

buggymcbugfix avatar Aug 29 '22 13:08 buggymcbugfix