Rust 101 with cargo

Akira Kakkar
2 min readMay 18, 2020

--

Overview:

I have started learning Rust today and decided to output for my self-memo here. Dug a bit on tooling around rust especially cargo. Cargo is Rust’s build system and package manager that simplifies how we work with Rust.

Install:

Before starting on the project, you have to install Rust. I have only MacOS so anything I am writing only for MacOS.

curl https://sh.rustup.rs -sSf | sh

Starter Code

Of course, we start with hello world whenever we start learning new language. The main function is special that it runs first in every executable. println! calls a Rust macro instead of function with “!”. I will write later with what Rust macro in different article.

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

You can create the same file with

cargo new project_name

“cargo new” also creates toml file that maintain dependency, version, etc.

Build:

You can compile Rust by running

rustc main.rs

or you can build with Cargo

cargo build

Run:

You can run executable by Running

./main

or you can run with Cargo (This does both build and run)

cargo run

Cargo check:

“This will essentially compile the packages without performing the final step of code generation” from the official document. This is used to “check” if build can run successfully. We want to run this periodically to confirm if code is compilable since build typically takes longer.

cargo check

Others:

These two commands are something I found it interesting. This command will update dependencies in the Cargo.lock file to the latest version. To me, this is updated version of what npm is doing. npm maintains lock file as well and that holds the minor versions.

cargo update

This another cool feature you can use when you are developing. This opens up docs of all of dependencies. You don’t have to type name of function in the search engine anymore.

cargo doc — open

Reference:

Book: The Rust Programming Language (Covers Rust 2018)

Web reference: https://doc.rust-lang.org/book/title-page.html

--

--

Akira Kakkar
Akira Kakkar

No responses yet