Search code examples
rustrust-cargo

Is there a cargo command exclusively to download crates to the registry directory?


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).


Solution

    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.