How can I compile the crate and use it in other crate?

I am beginner and I don't know how can I compile the crate and use it in other crate? like c++ that I can pre-compile the library and use it later.

Are you familiar with cargo? That will automatically compile the dependencies as they're needed. Rust doesn't have a stable ABI, so it's not guaranteed that a library built will be able to be used by another crate later.

2 Likes

Note that you can use cargo with your own crates - instead or in addition to the version, use a path to specify the dependency.

2 Likes

Precompile the library and use it later in C++ requires both the library and the binary are compiled with the same compiler binary. It's neither reliable nor recommended. The Rust have similar restriction, but it have a first party build tool/package manager.

The Cargo is the first party build tool/package manager for the Rust. It's recommended to use it instead of the rustc, unless you're writing code on some exotic architecture without the Cargo support like those internet routers. You write the Cargo.toml file, and the Cargo fetches those dependencies from various sources including the crates.io, git repositories, and local file system.

https://doc.rust-lang.org/stable/book/ch01-03-hello-cargo.html

https://doc.rust-lang.org/stable/cargo/

2 Likes