Rust Crates

A crate is a unit that contains a set of related modules, functions, types, and other code that can be compiled into an executable binary or a library.

Crates in Rust are similar to libraries or packages in other programming languages. A crate can produce an executable or a library, and can be shipped using Rust’s package management tool, Cargo.

Every crate has a root module that contains all of the code contained in the crate.

What is a Crate?

A crate is a unit of code organization and reuse in Rust lang. It is similar to a package or a module in other programming languages. However in Rust, a package is a collection of one or more crates that collectively provide a set of functionality.

Crates can be published to crates.io – a central repository where crates can be shared with other Rust developers. Crates can also depend on other crates, allowing for modular and reusable code.

Every crate has a name, version, and dependencies, which are defined in the cargo.toml file. The cargo.toml file the manifest file of the Rust package manager, Cargo.

Let’s learn a bit more about crates.io and cargo.toml so that we can discover how crates work.

Crates.io

Crates.io is the central package registry for Rust. It is a public repository where developers can share and discover Rust crates.

Crates.io provides a convenient way for Rust developers to manage dependencies and share code with the Rustacean community. Developers can easily search for and download crates from the registry, and can also publish their own crates for others to use.

Crates on crates.io are versioned, which allows developers to specify which version of a crate they want to use in their project. Crates can have dependencies on other crates, allowing for modular and reusable code.

Overall, crates.io is an important part of the Rust ecosystem, enabling easy sharing and discovery of Rust crates and promoting code reuse in Rust projects.

Cargo and Cargo.toml

In order to use crates from crates.io in a project, developers need to add the crate’s name and version to their project’s Cargo.toml file.

Cargo.toml is the manifest file for Rust projects managed by the Cargo package manager. Whenever a project is built, Cargo automatically downloads the required crates from crates.io and includes them in the project’s dependencies.

The Cargo.toml file is written in TOML , which stands for ‘Tom’s Obvious Minimal Language’. TOML is a simple configuration file format that is easy to read and write. The Cargo.toml file lives in in the root directory of a Rust package.

Cargo.toml includes a bunch of important information, such as:

  • Name and version of the package.
  • Package authors.
  • Dependencies on other crates – including their names and versions.
  • Build scripts for the package, and their configurations.
  • License and copyright information.
  • Metadata, such as description and homepage.

The Cargo package manager reads the Cargo.toml file to determine the package’s metadata and dependencies. Cargo then uses that information to download and build the necessary crates for the package.

Developers can also use Cargo commands to manage their Rust projects, such as building, testing, and publishing the package to the central registry, crates.io.

Generating a Crate With Cargo

We can use the ‘cargo new‘ command to generate a crate using Cargo:

$ cargo new new_crate
$ cd new_crate

This creates a new crate called ‘new_crate’.

Building a Crate

The ‘cargo build’ command can be used to build a crate:

$ cargo build

Importing and Using External Crates

We can import crates by placing code in the src/main.rs file using the extern keyword:

extern crate external_crate_name;

This tells Rust to compile and link to external_crate_name.

Then we can use the scope resolution operator ‘::’ to access the external crate:

external_crate_name::outer_module::inner_module::function();

In this example, we are accessing function(), which happens to live inside inner_module, which itself lives in outer_module.