Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I share common code between Rust projects without publishing to crates.io?

There may not be a good answer for this question, but I have code that I would like to share between two different Rust projects WITHOUT publishing the crate to crates.io.

The code is proprietary and I don't want to put it out into the wild.

like image 995
mattforni Avatar asked Jun 02 '16 03:06

mattforni


People also ask

Where does rust store downloaded crates?

registry/src If a downloaded . crate archive is required by a package, it is unpacked into registry/src folder where rustc will find the . rs files.

What is use crate in Rust?

Keyword crate A Rust binary or library. The primary use of the crate keyword is as a part of extern crate declarations, which are used to specify a dependency on a crate external to the one it's declared in. Crates are the fundamental compilation unit of Rust code, and can be seen as libraries or projects.


1 Answers

but it's proprietary code and I don't want to put it out into the wild.

You don't have to publish a crate. Specifically, just create the crate (cargo new shared_stuff) then specify the path to the common crate(s) in the dependent project's Cargo.toml:

[dependency.shared_stuff]
path = "path/to/shared/crate"

The Cargo documentation has an entire section on types of dependencies:

  • Specifying dependencies from crates.io
  • Specifying dependencies from git repositories
  • Specifying path dependencies

I believe that Cargo will allow you to fetch from a private git repository (such as on Github or another privately hosted service, such as GitLab), but I haven't tried that personally. Based on my searching, you will need to have previously authenticated or otherwise configured git to not require an interactive password entry.


It's theoretically possible to create your own crate registry. I've not even attempted to do this, but the machinery is present in Cargo to handle it.

like image 127
Shepmaster Avatar answered Nov 03 '22 03:11

Shepmaster