Components of a Rust Package
A Package in Rust comprises three key components.
- The actual source code of the application. This can include external imports.
- 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.
- 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:
Running the command above creates a new directory with the package name specified. Inside the directory, you will find other files and directories:
The file and directory tree are as shown:
├── Cargo.toml
└── src
└── main.rs
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 main.rs 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:://to_url_website.com"
readme = "readme.md"
license = "MIT"
You can learn more about various keys and their corresponding values in the resource below:
https://doc.rust-lang.org/cargo/reference/manifest.html
Cargo Build Package
Once you are done with your code and configuration, you can build your package. The command is as:
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.toml
├── src
│ └── main.rs
└── target
├── CACHEDIR.TAG
└── 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:
https://doc.rust-lang.org/cargo/commands/cargo-build.html
Cargo Run Package
Once you have the package completed, you can execute it using the cargo run command:
This will build and execute the package for you. Output from hello world package is as shown:
Conclusion
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!!