Getting Rusted – Part-2

In this post  I’ve show you how to create your first rust language project using Cargo ,

I think you have already installed and configured rust language in your computer according to my previous post Install Rust on your machine.

So first move to your preferred directory to create a project and issue the following command.

user@user-laptop:~$cargo new RustTtest

Here cargo will create the project folder within seconds 🙂 .

Screenshot at 2020-07-18 09-44-04

Then move to the  project directory using  following command

user@user-laptop:~$ cd RustTest 

After that issue the following command to view the folder structure of the rust project.

user@user-laptop:~/ RustTest$ tree 

Then we can see the following structure in the terminal window ,

Screenshot at 2020-07-18 10-10-24

This is the simple basic structure of a rust project . Then let’s get a closer look at “Cargo.toml” . This is called a manifest or dependency descriptor file . It is like “package.json”   in npm project or “composer.json” in laravel project .

We can see the contents of “Cargo.toml” after issuing the following command .

user@user-laptop:~/ RustTest$ cat Cargo.toml 

So that file consists of the followings ,

Screenshot at 2020-07-18 10-28-39

Here we can see that there is separate section(s) for package description , dependencies description . Here we can modify package name,version number , author etc , also the dependencies which need to extend the capabilities of the project.

Then we look at the src folder contents using following command ,

user@user-laptop:~/ RustTest$ cat src/main.rs 

Here we can see code as below,

fn main() {
    println!("Hello, world!");
} 

Here   “fn main ()” describes the main function of the code and function body is describes within  the curly brackets “{}”

“println!” command basically outputs string or variable data .

Then we must compile the project before run by issuing the following command

user@user-laptop:~/ RustTest$ cargo build

you can see the following compiler output in the terminal

Screenshot at 2020-07-18 10-53-58

After build the project if you look at the project directory we can see that there is newly created directories and files . The compiled project is at the “/target/debug” directory.

Screenshot at 2020-07-18 10-58-54

Here we can run the compiled binary file via issuing the “cargo run” command or directly run the project using following command .

user@user-laptop:~/RustTest$ ./target/debug/RustTest

Screenshot at 2020-07-18 11-06-30

Screenshot at 2020-07-18 11-11-03

Ok 🙂 , this is how to create and compile your first rust project . Hope you’ll get the idea 🙂 .

If you have any issue please comment here 🙂 !

have a nice day 🙂 ! .

Getting Rusted – Part-2