Rust Lang

How to Use Rust Packages

In Rust programming, or cargo, a package refers to a collection of files written in Rust. Think of a package as a collection of files that make up an application. Using packages, we can bundle up all the modules that create an executable or library to create a package.

Components of a Rust Package

A Package in Rust comprises three key components.

  1. The actual source code of the application. This can include external imports.
  2. A library file that defines the libraries the application requires. This is an optional file and does not need to be present if your package does not require any libraries.
  3. A Cargo.toml file which holds the configuration for your components. This config file is very useful as it describes how to bundle various components of the package into an application.

Welcome to Cargo

When working with packages, there is one tool you need to know: cargo. Cargo is a “package manager” for Rust. It allows for building and managing of Rust packages. It performs operations such as building creates, installing and uninstalling crates, deployment, etc.

NOTE: Although the term package and crate can be used interchangeably, they are not the same thing.

Let us discuss how we can work with cargo to create and manage Rust packages.

Cargo Create Package

To create a new Rust package, use the cargo new command followed by the name of your package:

$ cargo new package_name;

Running the command above creates a new directory with the package name specified. Inside the directory, you will find other files and directories:

$ tree package_name

The file and directory tree are as shown:


├── Cargo.toml

└── src


1 directory, 2 files

In the root directory of the package_name, you will find the Cargo.toml file which is a default config file generated by cargo.

The next is the src directory, which contains the source code for your application. By default, cargo will add file with a hello world program.

That is the bare minimum for deploying a package. If you want to build a more complex application, ensure the source code is stored in the src directory.

You can also perform configuration in the cargo.toml file. Popular options you can set are as shown in the example toml file.


name = "package_name"

version = "0.1.0"

edition = "2021"

authors = ["linuxhint", "csalem"]

rust-version = "1.56"

description = "package description"

documentation = "path:://"

readme = ""

license = "MIT"

You can learn more about various keys and their corresponding values in the resource below:

Cargo Build Package

Once you are done with your code and configuration, you can build your package. The command is as:

$ cargo build

Cargo will build and generate the required executable files and store them in the target directory.

The cargo build command will also add a few directories and files as shown:

├── Cargo.lock

├── Cargo.toml

├── src

│   └──

└── target


└── debug

├── build

├── deps

│   ├── package_name-b747d5f1560878a7

│   └── package_name-b747d5f1560878a7.d

├── examples

├── incremental

├── package_name

└── package_name.d

You can learn more about the generated files and directories in the resource below:

Cargo Run Package

Once you have the package completed, you can execute it using the cargo run command:

$ cargo run

This will build and execute the package for you. Output from hello world package is as shown:


In this guide, we covered Rust packages, how to create a package, build it and run it using cargo. You can check how to deploy packages to crates in the official documentation.

Thanks for reading & Stay Rusty!!

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list