Part-2: Introduction to Cargo🚢
Tags: rust, programming, tutorial, learning
Published: 2024-01-19 10:30:20

Hey folks, welcome to part 2 of my blog series on rust, where we'll learn the rust programming language from scratch. If you haven't read the first part then, i recommend you to read that first to install rust on your machine. Read part-1 [here](https://dev.to/codezera/part-1-getting-started-with-rust-3i40).

Lets start with part-2 of our series!

---

# What is Cargo?

If you are familiar with nodejs and npm then its very easy for you to understand, `cargo to rust` is what `npm is to js`.

Cargo is Rust’s build system and package manager. Most Rustaceans, yeah this is what rust devs call themselves, the one who has written `hello world` is also a proud rustacean. 

Rustaceans, use this tool to manage their Rust projects because Cargo handles a lot of tasks for you, such as building your code, downloading the libraries your code depends on, and building those libraries.

If you don't know what a package manager is then no worries let me explain, basically the idea here is no one should write everything by themselves, especially the code which other have written in a more effective way, suppose you are making a random number generator, now there are two options: one is you write all the logic by yourself or by installing something called a package in your code, which is nothing just some lines of code which will do your work very easily.

If still the picture is not clear to you, then i'll suggest just be patient, google things out and you'll understand it by time.

To check the verson of the cargo, place the following command  in your terminal:

```
cargo --version
```

---

# Creating your first project with Cargo

Let’s create a new project using Cargo and look at how it differs from our original “Hello, world!” project we discussed in our last blog. Enter the following command in your terminal and press enter:

```
cargo new hello_cargo
```

This will make a new folder called hello_cargo, now go to that folder by using the following command:

```
cd hello_cargo
```

Your directory should look like the following:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d5tb4ekg5854ib9z6q74.png)

Lets understand this folder structure a bit,`src` folder is the place where all of your rust code will live. Initially it will have a `main.rs` file which will have the following code:

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

If you remember this is the same code which we wrote manually in our last blog. This code just prints `Hello, world!` in the console.

Moving on, you'll see a `.gitignore` file which if are familiar with git then you already know what it does. If not, then i highly highly recommend learn git, its a mandatory(not really but its like abcd for devs) skill for developers.

Next you'll see a `Cargo.toml` file, this file is in the TOML (Tom’s Obvious, Minimal Language) format, which is Cargo’s configuration format. This file is similar to `package.json` file, if you've used node before. This file basically have information about your project, for example name of your project, its version and edition. 

Let me explain you the structure of this a bit, The first line, [package], is a section heading and whatever is below that will come under package until there is another section heading. As we add more information to this file, we’ll add other sections. You can see [dependencies] section heading this will contain all of your project dependencies or packages you'll install in future.

---

# Building and Running a Cargo Project

For building and running there are two approaches, lets see both of those:

## 1. Manually Building and Running

To run the project you need to first build the project and then run it. To build the project use the following command:

```
cargo build
```

This will make you a binaries build which you can execute or run. Now when you'll run this command it will make a file inside the `./target/debug` called hello_cargo, this file has the binary for the project. To run the binary you need to run the following command:

```
./target/debug/hello_cargo
```

If everything works fine, you'll see a `Hello world!` in the console.

## 2. Directly running the project

There is another command through which you can directly build and run the project. To do so run the following command:

```
cargo run
```

This command will directly build and run the project, and this command is used a lot when you're developing a project in rust.


# Special Section

When your project is ready for release, you can use the following command:

```
cargo build --release
```

This command will build the project with optimizations. This command will create the binaries file inside the `./target/release` directory instead of `./target/debug`. These optimizations will make your rust code run faster. This command is not used while developing because it takes a bit longer than the normal `cargo build` command.

---

# Why even use Cargo?

You might be thinking, why do we even need the cargo, why can't we write everything directly, like we did in our last blog. For now it won't make much sense but when your code will grow, cargo will help you a lot in managing your code.

---

# Thats it!

You're already off to a great start on your rust journey, i hope you like this series, if you do then do like the blog and comment down below if you need any improvements in the blog.

Thats it for today, see you guys in the next one. Till then!

Happy Coding!

---

You can find me on twitter [here](https://twitter.com/yadav_bhavesh73).

---