RUST Programming Language – What do You Need to Know in 2022

I come across this programming language as I was exploring the Solana cryptocurrency smart contract development this year. Before we dig into the smart contract, I need to understand this language first as this is quite intimidating as I am coming from C# and the syntax is weird and this is dealing with low-level language similar to C++ or C πŸ™‚

Background

Use the following environment:

  • Windows 11
  • WSL installed
  • Visual Studio Code installed

Facts

Rust is a system programming language and a very low-level language similar to C++ and C.

Rust has no garbage collector.

Rust is a memory safety guarantee ie. dangling pointers, null pointer exceptions, data races, iterator invalidation and more.

Rust is like C and it’s not Object-Oriented. The β€œclass” is using the β€œstruct” and the implementation is using imp<>.

Access WSL

After installing WSL on Windows 11, to access the terminal, run the wt.exe

Run:

wsl

Install Rust

On the terminal, type in:

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

Update and Version

To update:

rustup update

To check the version:

rustc --version

Note: By the time this blog is written, the latest version is 1.60.0.

Run Visual Studio Code

Launch the Visual Studio Code.

Press Ctrl + Shift + X to launch the Extension.

Install the rust-analyzer extension.

Note: Do not install the Rust extension as this extension is a legacy and eventually will get replaced with a rust-analyzer as the main one. Rust-analyzer has an auto-completion, a definition pops up, code formatting, etc.

Creating the Project

On the Visual Studio Code top navigation, select the File > Open Folder

Navigate to the user /home folder. Click the Ok button.

On the Visual Studio Code top navigation, select the Terminal > New Terminal

Navigate to the folder:

cd ~/

Create the folder of the application domain, i.e: dewacorp, solana-labs, etc

mkdir test-app-domain

Go to the application domain:

cd test-app-domain

Create the application using Cargo:

cargo new test-app

Run the test app, you can either use:

Via Cargo and go to the right folder

cd test-app
cargo run

or you got the src:

cd test-app
rustc ./src/main.rs

This will generate a binary called the main

To run the binary:

./main

Delete the test application domain if no longer needed:

rm -r test-app-domain

In Visual Studio, open up the workspace for a specific application by going to open the folder and navigating the folder into the application. Click the OK button.

Refactoring for changing the variable, press F2 and change the variable. The rust-analyzer will replace the variable inside the scope and will check if there is a similar name.

Variables

The & indicates that this argument is a reference, which gives you a way to let multiple parts of your code access one piece of data without needing to copy that data into memory multiple times.

Concepts

Ownership

There are three solutions for managing the memory: Manual Memory Management (C, C++), Garbage Collection (Java and C#), and Ownership model (Rust).

The pros and cons of these three:

SolutionsProsCons
Manual Memory ManagementControl over memory
Faster Runtime
Smaller program size
Error
Slower write time
Garbage CollectionNo error
Faster write time
No control over memory
Slower runtime performance
Larger program size
RustControl over memory
No error
Faster runtime
Smaller program size
Slower write time

This concept is very crucial to understanding the memory – stack and heap. If you are coming from C# or Java stuff, this concept needs to be understood very clearly.

References

Tim McNamara – How to Learn Rust – https://www.youtube.com/watch?v=sDtQaO5_SOw&t=345s

The Rust Programming Language – https://doc.rust-lang.org/book/

Free Code Camp – Rust in Replit – https://www.youtube.com/watch?v=MsocPEZBd-M

Rust Language Community – https://www.rust-lang.org/community

Rust Discord – https://discord.com/invite/rust

Rust Users Forum – https://users.rust-lang.org

Getting Started with Rust in Visual Studio Code – https://www.youtube.com/watch?v=B1zIYKP_1Fw

Learn Rust by Ryan Levick (@ryan_levick) – https://www.youtube.com/c/RyanLevicksVideos

Learn Rust by Microsoft Developer – https://aka.ms/vscodelive/learn-rust

Rust Meet Up – https://this-week-in-rust.org

Microsoft Learn Rust – https://docs.microsoft.com/en-us/learn/modules/rust-introduction