Open In App

How to Add a Crate in Rust?

Last Updated : 09 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  1. Have some programming experience.
  2.  A machine with Rust configured will be needed to run demonstrations.
  3. 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.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads