Crates
💭 Crates are bit similar to the packages in some other languages. Crates compile individually. If the crate has child file modules, those files will get merged with the crate file and compile as a single unit.
💭 A crate can produce an executable/ a binary or a library.
src/main.rs
is the crate root/ entry point for a binary crate andsrc/lib.rs
is the entry point for a library crate.
01. lib.rs on executable crate
💡 When writing binary crates, we can move the main functionalities to src/lib.rs
and use it as a library from src/main.rs
. This pattern is quite common on executable crates.
💯 As I mentioned earlier, in here we use simplest examples to reduce the complexity of learning materials. But this is how we need to write
greetings/src/lib.rs
to make the code more testable.
📖 When importing a crate that has dashes in its name “like-this”, which is not a valid Rust identifier, it will be converted by changing the dashes to underscores, so you would write
extern crate like_this;
lib.rs
can link with multiple files.
02. Dependency crate on Cargo.toml
When the code in the lib.rs
file is getting larger, we can move those into a separate library crate and use it as a dependency of the main crate. As we mentioned earlier, a dependency can be specified from a folder path, git repository or by crates.io.
a. Using folder path
Let’s see how to create a nested crate and use it as a dependency using folder path,
b. Using git repository
If you want to use a library crate on multiple projects, one way is moving crate code to a git repository and use it as a dependency when needed.
c. Using crates.io
The other way is uploading it to crates.io and use it as a dependency when needed.
🚧 First, let’s create
a simple “Hello world” crate and upload
it to crates.io.
💭 //! doc comments are used to write crate and module-level documentation. On other places we have to use /// outside of the block. And when uploading a crate to crates.io, cargo generates the documentation from these doc comments and host it on docs.rs.
💡 We have to add the description and license fields to Cargo.toml, otherwise we will get error: api errors: missing or empty metadata fields: description, license. Please see http://doc.crates.io/manifest.html
To upload this to crates.io, 1. We have to create an account on crates.io to acquire an API token 2. Then run cargo login <token>
with that API token and cargo publish
📖 This is how it describes on Cargo Docs with more details.
You’ll need an account on crates.io to acquire an API token. To do so, visit the home page and log in via a GitHub account (required for now). After this, visit your Account Settings page and run the
cargo login
command specified.Ex.
cargo login abcdefghijklmnopqrstuvwxyz012345
The next step is to package up your crate into a format that can be uploaded to crates.io. For this we’ll use the
cargo package
sub-command.Now, it can be uploaded to crates.io with the
cargo publish
command.If you’d like to skip the
cargo package
step, thecargo publish
sub-command will automatically package up the local crate if a copy isn’t found already.
The name of our crate is test_crate_hello_world
. So it can be found on, 📦 https://crates.io/crates/test_crate_hello_world 📑 https://docs.rs/test_crate_hello_world
💯 crates.io supports readme files as well. To enable it, we have to add the readme field to Cargo.toml. Ex:
readme="README.md"
🏗️ Okay then, Let’s see how we can use this from an another crate.
By default, Cargo looks dependencies on crates.io . So we have to add only the crate name and a version string to Cargo.toml and then run cargo build
to fetch the dependencies and compile them.
Last updated