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

一曲冷凌霜 提交于 2019-11-30 16:44:17

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/37581993/how-do-i-share-common-code-between-rust-projects-without-publishing-to-crates-io

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!