Faster way to add dependencies?

For example how I behave when I need to install rand crate

  1. I have to go to https://crates.io/

  2. Enter package name I need

  3. Find package I need and copy it's name and version and paste it into Cargo.toml

Which takes a lot of time. It may seem like a trivial thing to do, but I am just too spoiled by c# that I can find package I need and install any of it's versions without leaving my IDE (nuget and it's vs code extensions is cool btw).

I need something like this

cargo crate add rand

--- finds a rand crate ---
--- finds a rand version that does not conflict with my current dependencies ---
--- if not found tells me that maybe I misspelled name and suggest similar ---
--- adds rand to Cargo.toml ---

And maybe something like this

cargo crate search rand

--- search for crates similar names and give me their short descriptions ---

But I cannot find anything like that

Take a look at this tool: cargo-edit/README.md at master ยท killercup/cargo-edit ยท GitHub

3 Likes

The cargo-edit plugin cargo add has been integrated into Cargo and will be available in 1.62. You can try it out now on nightly:

โฏ rustup default nightly
info: using existing install for 'nightly-x86_64-unknown-linux-gnu'
info: default toolchain set to 'nightly-x86_64-unknown-linux-gnu'

  nightly-x86_64-unknown-linux-gnu unchanged - rustc 1.63.0-nightly (420c970cb 2022-06-09)

โฏ cargo new foo
     Created binary (application) `foo` package
โฏ cd foo
โฏ cargo add rand
    Updating crates.io index
      Adding rand v0.8.5 to dependencies.
             Features:
             + alloc
             + getrandom
             + libc
             + rand_chacha
             + std
             + std_rng
             - log
             - min_const_gen
             - nightly
             - packed_simd
             - serde
             - serde1
             - simd_support
             - small_rng
โฏ cat Cargo.toml
[package]
name = "foo"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rand = "0.8.5"
4 Likes

To clarify, only cargo-add has been integrated from cargo-edit

  • #10520 for tracking adding cargo rm. I have a mentee lined up for this that will hopefully start soon
  • #10498 for tracking adding cargo upgrade. I feel a lot less certain of what the UI should be for this:
    • In cargo update or a new command and how do we make the names not confusing
    • The current flags partially mimic cargo but in other ways reuses existing flags for other purposes

It has been less clear how much of an interest there would be in cargo set-version, so I have not been exploring merging that upstream.

2 Likes

finds a rand version that does not conflict with my current dependencies

The only conflict-avoidance cargo-add does is that it will reuse a version for a dependency that it finds in another dependency table (e.g. cargo add --build rand and later doing a cargo add --dev rand).

If people have ideas on how to further avoid conflicts, I'd recommend opening an issue. Maybe once we get public dependencies, we could rely on those (commented on the tracking issue).

if not found tells me that maybe I misspelled name and suggest similar

At the moment, we do not offer spelling suggestions. Feel free to create an issue about it. Help catch typo squatting when adding dependencies ยท Issue #10655 ยท rust-lang/cargo ยท GitHub is a similar issue.

cargo crate search rand

cargo search exists. Unsure how well it handles misspellings. Help catch typo squatting when adding dependencies ยท Issue #10655 ยท rust-lang/cargo ยท GitHub also talks a little about this but a dedicated issue would be useful.

2 Likes

Worth mentioning that (if you're willing to pay for an ide), Jetbrains' Rust support includes pretty good completion on cargo.toml, which means you don't need to go anywhere if you already have an idea of the crate name or lookup the version.

For neovim users there is crates.nvim. cargo add has been getting me to think that a cargo LSP would provide a really nice experience.

2 Likes