Rust is a multi-paradigm, general-purpose programming language designed for performance and safety, especially safe concurrency. Rust is syntactically similar to C++, but guarantees memory safety by using a borrow checker to validate references. Overall, it is a language worth learning with a supportive community and hereon in I’ll be documenting my Rust learning journey !

Installing & Understanding the Ecosystem.

🧑‍💻 Go to rust-lang.org, and simply follow the instructions.

  • Install and manage Rust using the rustup tool. If you already have Rust installed & want to check the version, use :
1
$ rustc -V
  • rustc is the Rust compiler. To update to the current stable version of rustc compiler, use :
1
$ rustup update
  • If you’ve installed Rust using rustup, it’ll come with cargo, the Rust package manager.

Using cargo is stupidly simple,

  • To create a new package (project) with cargo, use
1
$ cargo new project_name --bin

This creates a rust project with a binary program as output.

1
2
3
4
5
6
$ cd project_name
$ tree .
.
├── Cargo.toml
└── src
    └── main.rs

This program can be ran using cargo run command while in the project directory.

1
2
$ cd project_name
$ cargo run 

While creating Library package, use cargo new with --lib

1
$ cargo new lib_name --lib

follow the same procedure as the binary program to compile the library

1
2
$ cd lib_name
$ cargo run 

That’s about it !! You can use your favorite text editor (Vim, VS Code, etc.) with rustfmt (Rust code formatter) or RLS (Rust Language Server) or my favorite Rust Analyzer.

Rust is an opinionated language, it always helps to have these tools while developing.

Learning Resources

Rust language has excellent and completely free learning resources such as 🔽

[1] The (official) Rust Language Book
[2] Rustlings - 🦀 Small exercises to get used to reading and writing Rust code!
[3] Rust by Example - Illustrates various Rust concepts & std-lib with examples!

I’ve found Jason Orendorff and Jim Blandy’s book Programming Rust: Fast, Safe Systems Development very useful and would highly recommend it to anyone who seriously wants to learn Rust.

Greeting world (as per tradition) !

hello world

“Hello World!” by Brian Kernighan

To print “Hello World!”, a string of type &str in Rust we need to use what Rust calls macros. Macro used in Rust to print with newline (\n) character is println! (note Rust macros end with ! (bang)).

Let’s create package/crate named ‘hello’ for this simple project. Using cargo ,

1
2
$ cargo new hello
$ cd hello

This will create package with name ‘hello’. Now edit the src/main.rs file to 🔽

1
2
3
4
5
// main.rs

fn main() {
    println!("Hello World!");
}

To run this, use cargo run command. The output should resemble this 🔽

terminal