Skip to content

RFC: Add example of cargo add#3331

Closed
buggymcbugfix wants to merge 1 commit into
rust-lang:mainfrom
buggymcbugfix:main
Closed

RFC: Add example of cargo add#3331
buggymcbugfix wants to merge 1 commit into
rust-lang:mainfrom
buggymcbugfix:main

Conversation

@buggymcbugfix

@buggymcbugfix buggymcbugfix commented Aug 28, 2022

Copy link
Copy Markdown

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

  • 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?
  • Update the Cargo documentation to explain what the Features output means.

@carols10cents carols10cents added this to the ch1 milestone Aug 29, 2022
@carols10cents

Copy link
Copy Markdown
Member

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 rand@0.8.3 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.

@buggymcbugfix

Copy link
Copy Markdown
Author

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 rand@0.8.3 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 rand@0.8.3 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!

bors added a commit to rust-lang/cargo that referenced this pull request Aug 30, 2022
…epage

Very slight `cargo add` documentation improvements

As discussed in rust-lang/book#3331, a quick explanation of the `Features` part of the message that gets printed to stdout when adding some dependency.

Consider the following example:

```
cargo add my-crate
    Updating crates.io index
      Adding my-crate v0.1.0 to dependencies.
             Features:
             + foo
             - bar
```

It was not clear to me what `+foo` and `-bar` meant until `@carols10cents'` kindly explained it to me. Hopefully the documentation now clarifies this.

TODO:

- [x] Run `./build-man.sh`
@chriskrycho

Copy link
Copy Markdown
Contributor

@buggymcbugfix are you interested in pulling this apart into the relevant PRs and landing it? If not, I will also take care of that as part of the prep for the next print edition, but I wanted to give you the opportunity if you’d like to do it!

@wadeio

wadeio commented Jul 6, 2026

Copy link
Copy Markdown

@chriskrycho @carols10cents @buggymcbugfix It's 2026 now. I observed that this PR is still open. Would you mind telling me when this PR might land?

@wadeio

wadeio commented Jul 6, 2026

Copy link
Copy Markdown

I think using cargo add is more convenient than modifying the toml file manually btw

@chriskrycho

Copy link
Copy Markdown
Contributor

I agree that cargo add is much more convenient. It looks like we missed pulling this in for the last print edition, unfortunately (one of no doubt several such misses! Books are tricky). I’d still be open to landing this PR or a variant on it.

@wadeio

wadeio commented Jul 12, 2026

Copy link
Copy Markdown

I know and understand you want to keep the online version of the book close to the print version when possible, as mentioned in the README file. But new versions between printed books often take a long time to come out. What about separating a new repo or branch that contains the latest version of the book so that learners can read the latest version?

@wadeio

wadeio commented Jul 12, 2026

Copy link
Copy Markdown

@chriskrycho

@buggymcbugfix

Copy link
Copy Markdown
Author

It's worth considering that this is a purely additive change. Merging this wouldn't make the info in the print edition wrong, we are merely providing some extra info to online readers. Probably best to merge this now, lest it gets missed again for the next print edition!

@carols10cents

Copy link
Copy Markdown
Member

This PR is out of date-- the main branch now uses rand 0.10.1. #4756

Also, please stop with the entitled comments about how long this PR has been open.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants