Open In App

Rust – Conventions

Last Updated : 02 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In Rust, there are certain conventions that need to be followed when we are creating dependencies. There are dependencies on libraries in Rust. Cargo! is a dependency manager in Rust that is similar to npm (in JavaScript) and pip (in Python). All the project dependencies are handled by cargo.

For initializing a new project in Rust, we use cargo.

Example 1:

Rust




// Rust code for conventions
cargo new gfg


Output:

 

After the commands are executed, the file hierarchy is this:

gfg
├── Cargo.toml
└── src
   └── main.rs

Explanation:

The root source of the file is main.rs. The only difference with other languages is the addition of cargo.toml config file for the project gfg

In Rust, when there are situations having requirements to have two binaries for the same project then we can add additional binaries as per our requirement in the bin folder. For example, we have placed two new files in the folders new_bin_folder_one.rs and new_bin_folder_two.rs respectively.

 

This is supported by cargo in Rust  and for compiling in a different folder we need to execute this command:

cargo run --bin new_bin_folder_two

The final file structure looks like this:

gfg
├── Cargo.toml
└── src
   ├── main.rs
   └── bin
         └── new_bin_folder_one.rs
         └── new_bin_folder_Two.rs

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

Similar Reads