DEV Community

Vijesh Salian
Vijesh Salian

Posted on

Starting with Rust - Cargo

In the last post, we saw how to compile a rust file ( .rs file) with the rust compiler. Here's that post in case you are interested.

In real world, applications that programmer writes can be made of several files. These files are grouped together in what we call a project. It is very common to have multiple projects in an application. In most cases, more than one developer is involved in building the application. Each project has a clear responsibility. Rust files serving a similar responsibility are generally in the same project. It is common that one Rust file could be dependent on another. It is also common that one Rust project could depend on another. If project X is dependent on project Y. Then Y is a dependency of X. A Rust project coud have multiple dependencies and it should list them and know them. The dependency however will not and should not know about the projects depending on it. Sometimes a project may have a dependency which was written by developers outside your team/company. They can make that dependency available in a central repository so that you can download and use them.

So, when working with Rust you will be compiling, downloading dependencies, building a lot. This is where Cargo comes in to picture. If you have been programming in other languages, all this may seem obvious and you possibly already know what Cargo can do.

How do I use Cargo?

First lets check Cargo is installed correctly. Fire up your shell.

cargo --version

If you see a version number, you are good to go. If not, search engine is your friend.

cargo new hello_rust

This is a command to create a new project. We created a new project called hello_rust. This creates a directory with the project name. If you look into the directory you will find that Cargo initialized it with a git repo. You can choose not to do so if you want. Then you will find a Cargo.toml file. This file is the configuration file for your project. In the project directory you will also find another directory called src. This contains the Rust code files.

Cargo.toml file contains metadata about the project and a list of dependencies if any.

The src directory contains a Rust file which you are familiar with.

Okay, what else can I do with Cargo?

Well, we can build and run our code with Cargo. Cargo knows how to make use of the rustc compiler.

In your shell, be at your project directory and run

cargo run

This commands builds and runs your projects with just one command.
This may look trivial now, but as your projects grow bigger, the benefit of Cargo becomes more visible. By this, Cargo provides a uniform way of building and managing dependencies, for all projects big and small.

If you want to quickly check if your code compiles, but don't want to run it, then use this command.

cargo check

If you want to compile and build an executable, but not run it, then use this command.

cargo build

If you want to ship your program, use this command to build with release mode to create a shippable program.

cargo build --release

Nice, so what else can I do with Cargo?

If you want to know more about cargo, you will find the Cargo book extensive.
Go here for the Cargo book. https://doc.rust-lang.org/cargo/index.html

Thank you for your attention.
Cheers 🍺

Top comments (2)

Collapse
 
deciduously profile image
Ben Lovy

Great write-up! Honestly, cargo is one of the biggest selling points of Rust from a usability standpoint.

Dependencies are one of the things I think Rust does right in a way I've never seen before. Cargo takes care of conflicting library dependencies automatically for you and you never need to think about what your direct deps depend on themselves. This is possible because of Rust's compiler name mangling scheme - each compiled function's mangled name ends up with information about the specific build and crate version it was from, so it's possible to have multiple conflicting versions of a function compiled in to your binary without any issues! The correct version will always be referred to by the caller. When you compile a new crate, you might see the same crate name with multiple versions come up in the list - it's incredibly cool that Rust is able to manage this in stride.

Collapse
 
vijesh_salian profile image
Vijesh Salian

Thank you for the informative comment.