How to Add a Crate in Rust?
Last Updated :
23 Jul, 2025
Rust is a highly performant, popular system programming language which means it is mostly used for developing low-level software like operating systems, drivers for hardware, game engines, etc.
But you will think that we have already been developing these kinds of software using C/C++ then why a new language, that's true now rust which was introduced in 2010 by Mozilla is a fairly new programming language as compared to C/C++ but it comes with tons of features or capabilities that C/C++ lacks. Some of them are memory safety, concurrency, expressive syntax, etc which have led to increasing popularity among developers.
A crate is the smallest amount of the code that the Rust compiler considers at a time for compilation, in other words, crates are the compilation unit in Rust. Crates can be understood as binaries or libraries that provide a set functionality and when crates are bundled together it is called a package.
You can even get your crates published on the official Rust registry called crates.io. This will allow other developers to download and use the crates you created in their projects. In this article, I will be covering a simple demonstration by creating a guessing game that will show how we can add a specific crate to a Rust project.
Prerequisites
To completely understand this article following prerequisites are expected:
- Have some programming experience.
- A machine with Rust configured will be needed to run demonstrations.
- Not necessary but some experience with Rust is appreciated.
Project Setup
To add a Crate we need a project so let's set up a project, I'm considering you have Rust installed and configured to be used on your machine if not please refer to this article Rust-Installation.
Creating a project in Rust is fairly simple just run the following command,
$ cargo new <project_name>
Output:
The following files is been created as an output of the above command,
Let's test the project created by executing the source which can be done as follow,
$ cargo run
Output:
Adding a Crate
Now that we have a simple Rust project configured let's tweak it to make a number-guessing game to do that add the following Rust code to the main.rs file,
Rust
use std::io;
use rand::Rng;
fn main() {
println!("Guess the number!");
// picks a random number between 1 and 100
let number = rand::thread_rng().gen_range(1..=100);
println!("Pls input your guess: ");
// variable for guess input
let mut guess = String::new();
// reading guess number using io
io::stdin().read_line(&mut guess).expect("Failed to read line");
println!("Guessed number was: {guess}");
}
As you can see we have used `rand` which is a crate from the rust registry that provides functionality such as generating a range of numbers and picking one from them. If you run the above code it won't run because we are using `rand` which is not unknown to our project as you see in the error below,
To resolve the error add the `rand` crate as a dependency in the Cargo.toml file which contains the configuration for your project. This can be done as follow,
Let's run the project again as we have added the required crates in the dependency, This time it executed without any errors we can also see the outputs though I ended up making a wrong guess.
Conclusion
Crates along with cargo package management and crates registry all work together to make the development process simple and prevent writing some code again and again.
Similar Reads
How To Build Your Career in Rust Why are so many companies adopting Rust as their preferred language? What does it take to learn Rust? If you are in touch with the latest developments in technology, you would find that this programming popularity is blooming faster than people liking Narendra Modiâs tweets (this may not be true). A
7 min read
How to Install rust on alpine? Rust is a multi-paradigm, general-purpose programming language designed for safety and performance, especially safe concurrency. It is notable for enforcing memory safety. It is syntactically similar to C++ and is considered a systems programming language with the mechanisms for low-level memory man
2 min read
Rust - From and Into Traits In Rust, we have the From and Into Traits that are linked internally and are implemented in type, conversion scenarios say in scenarios where we convert from type A to type B. The syntax for From Trait: impl trait From<T>Â {Â Â fn from(T) -> Self;} From traits in Rust are mainly used for eva
3 min read
Rust Use Declaration In Rust, we have a concept of the 'use' keyword. This keyword is used for importing or at times renaming items from other different modules or crates. Use the keyword to find its usage when we are required to import items or rename items from crates or modules in Rust programs. For precisely calling
4 min read
How to Become a Rust Developer? Rust developers are highly sought after in the tech industry due to Rustâs focus on memory safety, performance, and concurrency. Companies like Mozilla, Dropbox, Amazon, Microsoft, and Cloudflare actively hire Rust developers to build reliable and efficient software systems. Rust is used in a variet
11 min read
How to create a container using Podman? The emergence of Podman as a powerful engine for containers without daemons has presented a very good alternative to Docker. Always having your Podman installation up to date means that you will have the latest features, bug fixes, and security enhancements. This guide will take you through the step
3 min read