DEV Community

Cover image for CARGO: Rust's Package Manager (Think npm or pip)
Aditya Verma
Aditya Verma

Posted on • Originally published at aditya-22.hashnode.dev

CARGO: Rust's Package Manager (Think npm or pip)

Building a CARGO Project

In the last article we learnt how we create a new CARGO project using the cargo new command in the command line. Now it’s time to build that project. We will have a main.rs in the /src folder that was created when we executed the cargo new command. It contains code for basic ‘Hello, World!’ program that we build in the last article. Let us use that as a base for now, we will code later on.

Before building the project let us first understand what the term ‘building’ means? So the code that we wrote is in a human readable format. Now to convert this code into a machine understandable code and then into an executable that can run directly we ‘build’ the project. So to lay it out plainly we build our program from a cluster of .rs files into a single .exe file (if we are on Windows, on Linux and on Mac they will be different) that will run our code and we do not need to compile it every time.

Now that we are a little clear on what building a project is let us move to actually building our cargo project. To build a cargo project we need to run the cargo build command in the new folder that was created when we ran the cargo new command. In my example I named the project as ‘cargo_intro’, so I will do cd cargo_intro and then cargo build . This will create an executable file in the ./target/debug folder with the same name as the project which in my case is ‘cargo_intro’.

$ cd cargo_intro
$ cargo build
   Compiling cargo_intro v0.1.0 (<folder_path>/cargo_intro)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 3.98s
Enter fullscreen mode Exit fullscreen mode

Running cargo build for the first time also creates a new file at the top level called Cargo.lock. This file keeps track of the exact versions of dependencies in your project.

Running a CARGO Project

After the build we will have an executable file ready for us to run. We can directly run it by clicking in the file explorer or finder. But since we are all nerds here we will use the terminal to run the program. We can do so by just going to the executable in our terminal, so in our example we will do something like :

$ ./target/debug/cargo_intro
$ Hello, world!
Enter fullscreen mode Exit fullscreen mode

Now learning this path and running it like this seems like a hassle if I had to do it more than 3 times. Cargo has made it easy for us to not do this and instead we just run cargo run and it will run the executable for us. Another use of this command is that it eliminates the build step for us. If there are any files changes it will first build and then run the executable for us. So instead of performing 2 steps, first cargo build and then ./target/debug/<executable_name> we simply do cargo run and cargo will build and run the executable for us.

For example, let’s say I change ‘Hello, World!’ to ‘Hello, my name is Aditya.’ then it should rebuild.

$ cargo run
   Compiling cargo_intro v0.1.0 (<project_path>/cargo_intro)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.36s
     Running `target/debug/cargo_intro`
$ Hello, my name is Aditya.
Enter fullscreen mode Exit fullscreen mode

Cargo also provides us with cargo check command to check if the code will compile successfully but it will not produce a new executable with the changed code.

$ cargo check
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.03s
Enter fullscreen mode Exit fullscreen mode

So that is the basics for now. This much will help us setup and run rust programs. We are good to move to actual programming concepts and creating projects to understand the deeper rust concepts. Thanks a lot if you have been part of this journey till now, from the next article we will start creating some interesting programs and learn concepts. Hope to see you there!

-Aditya

Top comments (0)