0

I know that when you add a crate to your Cargo.toml file and run cargo build, or when you vendor a crate to your local project folder, in both cases, the crate and it's dependency crates are first downloaded to the /path/to/home/.cargo/registry folder.

(And that then, you can add those crates to other projects offline, either normally or by vendoring, from the saved copies in /path/to/home/.cargo/registry).

  • First question : Is that is there a cargo command exclusively for adding a crate to the /path/to/home/.cargo/registry directory?

    • Something like Python's pip install package_name command.
    • The idea here is to download crates to the /path/to/home/.cargo/registry directory first. And then add dependencies to my projects from that, in offline mode (cargo build --offline and cargo vendor --offline).
  • Second question : Is there a command to search whether a crate is already "installed" in this way, in the /path/to/home/.cargo/registry directory? (the cargo search searches in crates.io).

Thanks.

PS: I first thought this was the cargo install command but the manual says it works only on crates that have executable targets (as opposed to, presumably, purely library crates).

Keyboard Penman
  • 162
  • 1
  • 11
  • 1
    `pip install` is not a command "exclusively for adding a crate to the registry directory" and I've no idea where you got that impression. `pip install` is more of a mix between `cargo add` and `cargo install`. – Masklinn May 06 '23 at 07:50

1 Answers1

2
  1. To download a crate and its dependencies to the /path/to/home/.cargo/registry directory, you can use the cargo fetch:
$ cargo fetch
  1. To search for a crate in the local registry directory, you can use the cargo search with the --registry flag:
$ cargo search --registry=/path/to/home/.cargo/registry crate_name

Before search, you'll need to create a local registry (a directory) and register it to cargo.

mkdir my_local_registry
cargo registry init --index my_local_registry # Now you can search
cargo publish --registry=my_local_registry # This will publish your package to my_local_registry

cargo search my_library --registry=my_local_registry

Better can also use file:// based absolute URLs:

cargo search <query> --registry=file://path/to/my_local_registry

To register a directory as a registry, you can also edit the Cargo.toml file as described here.

Jishan Shaikh
  • 1,572
  • 2
  • 13
  • 31
  • `search` gives me an error "invalid character `/` in registry name: `path/to/home/.cargo/registry/`, characters must be Unicode XID characters (numbers, `-`, `_`, or most letters)". Also "the first character must be a Unicode XID start character (most letters or `_`)" when I put / at the beginning. – Keyboard Penman May 06 '23 at 07:19
  • 1
    Did you first create your registry using `crate registry init`? Editing my ans. – Jishan Shaikh May 06 '23 at 07:23
  • using `cargo` 1.70.0 and it doesn't have a `registry` subcommand. – Melendowski Jun 26 '23 at 14:34