How do you reuse your projects locally?

When I work on Java/Kotlin projects, I use Maven Publish Plugin's publishToMavenLocal task to make the project locally reusable.

I'd like to make my Rust projects reusable without publishing them to crates.io. Is there any way similar to publishToMavenLocal in the Rust world?

1 Like

I guess you could make a library and add it as a dependency in your Cargo.toml

Thanks, but I can't find how to use rlib in Cargo.toml.

I added mylib = { path = "./libmylib.rlib" } to the [dependencies] section in Cargo.toml, and got this error:

error: failed to get `mylib` as a dependency of package `mybin v0.1.0 (/home/rust/mybin)`

Caused by:
  failed to load source for dependency `mylib`

Caused by:
  Unable to update /home/rust/mybin/libmylib.rlib

Caused by:
  failed to read `/home/rust/mybin/libmylib.rlib/Cargo.toml`

Caused by:
  Not a directory (os error 20)

From my experience with Java/Kotlin projects, I'd like to avoid specifying "library files" (such as jar in java, and rlib in Rust) directly if possible. This is because doing so makes it difficult to notice when the latest version of the library has been released.

Thanks, but I can't find how to use rlib in Cargo.toml.

path dependencies need to be pointed at a package directory (has a Cargo.toml), not a .rlib file. Cargo always builds from source. You can read more about how to specify dependencies here: Specifying Dependencies - The Cargo Book

Another notable option in-between path dependencies and packages published to crates.io is git dependencies — you can specify a URL of any Git repository as a dependency, so you don't have to consider it significant or permanent enough for crates.io.

Note that with Git dependencies, just like crates.io dependencies, you have to run cargo update to get the latest version when the repository publishes new code.

8 Likes

I've confirmed that mylib = { git = "file://<path to mylib>" } works! Thanks!
EDIT: Confirmed that mylib = { git = "file://<path to mylib>", tag = "<specific tag>" } also works :+1:

2 Likes

If you're looking for an option similar to crates.io, you could consider running a private registry. There a few SaaS ones such as Shipyard but I prefer Kellnr so I can self host locally or on another machine using Docker.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.