How to Build and Manage Rust Packages with Cargo

 

Rust is well-known for being a programming language that focuses on security. But another standout feature of Rust is that, unlike many other languages, it provides its very own build system and package manager, which is known as Cargo.

Keep reading for a primer on how to manage Rust packages using Cargo.

 

What is a Rust package?

In Rust, a package is a set of files that can be used to deploy an application written in Rust. Rust packages have several components:

  • One or more Rust source code files, which contain the code to implement whichever functionality the package aims to supply.
  • Optionally, a library file, if the source code files require a library to run.
  • A configuration file, called Cargo.toml, that defines how the various other components of the package should be put together to deploy a usable application or service.

The source files and libraries are known as crates. Note that you may sometimes hear Rust developers use the terms “crate” and “package” interchangeably, but technically, there is a difference: A crate is an individual component of a package, while a package is what you get when you pair one or more crates with a deployment configuration file (i.e., the Cargo.toml file).

You can think of Rust packages as being akin to Docker images or Debian packages, with the main difference being that Rust packages include source code that must be compiled before it can be deployed.

 

What is Cargo?

Cargo is a tool for building and managing Rust packages. Cargo lets you create packages by combining crates together, as well as build and deploy those packages.

Cargo is similar to tools like Make (which can also be used to build Rust packages, if desired), but it is purpose-built by the Rust community for managing Rust packages specifically. That said, Cargo can be used to manage code written in other languages, including C, which is useful if you’re developing an application that includes other code in addition to Rust source code.

 

How to manage Rust packages with Cargo

Cargo’s syntax and methodology are quite straightforward.

Create a package

To use Cargo, you first need to create a package:

cargo new your_project_name

You’ll see that Cargo creates a new directory in your present working directory called your_project_name. The new directory contains a few things:

  • A default Cargo.toml file.
  • A src/ directory, which contains a file called main.rs. Main.rs is a Rust source file for a Hello World! program that Cargo creates by default.

Configure the package

You can go right ahead and build these files, if you just want to deploy Rust’s Hello World! program. To deploy a real-world app, however, you’ll first need to move your source file or files for the app into the src/ directory of your package:

cp /path/to/your/source/file.rs your_project_name/src

You’ll also need to modify Cargo.toml to fit the configuration of your application and any dependencies. Full details are at https://doc.rust-lang.org/cargo/reference/manifest.html.

Build the package

With your source code in place and your Cargo.toml file updated, you can build the package:

cargo build

The compiler will generate binaries and store them in a new subdirectory called target/ which exists alongside the src/ directory of your package.

Run the package

Now, you can run the application with a simple:

cargo run

Deploying Rust packages with Crates.io

At this point, you have a Rust package that you can run locally. But what if you want to deploy it to a production system (which is presumably not the same as your local build system) or share the application with other users?

That’s where Crates.io comes in. Crates.io is a public registry for hosting and distributing Rust packages. In other words, Crates.io is to Rust what Docker Hub is to Docker.

While there’s nothing technically stopping you from hosting compiled Rust packages elsewhere, such as on GitHub, Rust developers discourage this practice because they aim to make Crates.io the official, trusted package registry for the Rust community.

 

Enhancing Rust package management with Artifactory

You can get even more functionality and features out of Cargo by combining it with JFrog Artifactory.

In addition to interacting directly with packages hosted on Crates.io, Artifactory lets you create private, local Cargo repositories, where you have more control over access configurations than Crates.io supports. Local repositories are especially useful for businesses that need to share Cargo packages internally and want to avoid the security and performance issues associated with distributing them through a public registry.

You can also cache Cargo packages locally with Artifactory, which makes it easier to work with Cargo offline and improves performance by eliminating the need to download dependencies or other data from the Internet before building a new Cargo package. Artifactory supports version management for Rust packages, too, so that you can easily keep track of releases and archive older packages.

 

Related Resources: