Next, A Crabby Hello

ยท

8 min read

If you've learned any programming language before then you know how this goes. First, we'll learn how to say hello. I can't count how many times I've done this but it doesn't hurt so let's do it, and then some.

We'll look at:

  • What is Rust?

  • A Small Crustacean History

  • Stats

  • Rust and Its Uses

  • How to Setup Rust

    • Linux / Mac OS

    • Windows (with Visual Studio)

    • Windows (without Visual Studio)

    • Updating Rust

  • Creating and Running a simple Rust program.

  • Learning Resources: Where to go from here.

What Is Rust?

First of all, Rust is fast, very fast. It's a programming language designed to prevent common programming errors like null pointer dereferences (what the heck is that?), and data races.

Rust is good at managing memory too without the need for garbage collection (a process of freeing computer memory of data is no longer in use), which makes it efficient for systems programming where performance and control over hardware are crucial.

[!Hint] A Null Pointer dereference is something that happens when a program tries to access or manipulate some data through a pointer that's pointing to a null(non-existent or invalid) memory address.

Think of it like a explorer that takes you on an expedition to search for Big Foot(doesn't actually exist).

This bad because it can cause programs to crash or behave abnormally.

[!Note]- A pointer is a variable in programming that stores the memory address of another variable, So instead of holding a value it points to the location where the actual data is.

Rust is a language with the power of C++ and the syntactic sexiness (permit me ๐Ÿ˜‚) of JavaScript and Python.

A Small Crustacean History

Created at Mozilla by Graydon Hoare, Rust has gained a following since its creation in 2006. With its first stable release in May 2015 and a fast-growing, worldwide community to boast of, Rust is a programming language that has come to stay and is making strong strides in software engineering. Rust is consistently supported by the Rust Foundation.

Stats.

According to JetBrains State of Developer Ecosystem 2023 survey, Rust is the fastest growing programming language and most adopted this year. Rust has a bright future. Rust has been growing into a developer favorite for the last 5 years.

Rust and Its Uses

Rust is quite a versatile language and can fit into various domains of software development.

  1. System Programming - Rust is efficient at system-level programming, such as developing operating systems, firmware, and device drivers.

  2. Web Development - Rust can be used to build web applications and server-side components. Web can also be used to build client-side applications in combination with web assembly. Frameworks like Rocket and Actix provide these capabilities in Rust.

  3. Network Services - Rust is suitable for building network applications and services including networking protocols, servers, and clients.

  4. Game Development - Rust is earning rising popularity in the game development space due to its efficient performance and memory safety features. It can be used to build game engines, tools, and performance-critical components.

  5. Embedded Systems - Rust's control over system resources and minimal runtime make it applicable for embedded systems programming, where efficiency and reliability are crucial.

  6. Command-Line Tools - Rust is often used for building command-line utilities and tools due to its performance and ease of integration with existing systems.

  7. Blockchain and Cryptocurrency - Rust is used in the development of blockchain protocols and cryptocurrency projects. An example is the Solana blockchain protocol.

How to Setup Rust

Setting up Rust on your local machine for development can go from being a simple and easy task to being quite herculean, so let's look into setting up a developer environment for Rust.

Linux / Mac OS

If you're on a Linux or a Mac OS System, the only thing you'll need is your terminal. We'll be installing rust using the rustup command-line tool. Fire up your terminal, and type this:

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

This downloads a script and begins the installation of the rustup tool, which installs the latest stable version of Rust. You may be prompted to enter your password, but if all goes well you should see something like this in your terminal:

Rust is installed now. Great!

The script automatically adds the rust to your system PATH after your next login

To begin using Rust, run the following command in your shell:

$ source $HOME/.cargo/env

as an alternative, you could add this to your ~/.bash_profile

$ export PATH="$HOME/.cargo/bin:$PATH"

Since it's a Mac OS or a Linux operating system, you most likely have a linker installed already. If not check your platform's documentation for how to install a C/C++ compiler; They usually come with the correct linker needed. You may want to install a C compiler regardless of your need for only a linker; some common Rust packages depend on C code and will need a C compiler too.

After this, check if Rust is installed by running rustc --version in your terminal

Windows (with Visual Studio)

Go to the official Rust website and download the rustup installer, Next run the program and you should see a command-line interface like this:

[Pasted image 20231208125301.png]

follow the instructions on the interface (it's recommended to pick the first option and follow through). You'll have to install the Visual Studio IDE because it's the only way to install the Visual Studio C++ build tools

After this, check if Rust is installed running rustc --version inside PowerShell.

Windows (sans Visual Studio)

If your system for some reason can't run the Visual Studio IDE, you can install Rust without the need for the Visual Studio IDE by following these steps:

We'll be using the mingW compiler instead of the MSVC compiler that the Visual Studio IDE uses.

First, go to the msys2 and download the installer, and also download the rustup installer from the Rust official website.

Run the installer after the download. When the wizard runs, follow the instructions of the installer, it's relatively simple.

Once it is installed, a command-line terminal is opened

inside that command-line terminal, download the script that installs the MinGW compiler

$ pacman -S mingw-w64-x86_64-toolchain

some time into the installation, the installer will prompt for so permission to install some packages, enter "y" for yes and hit enter to continue the installation.

Next, we'll install base-devel . This is a package used to install tools for building a linker.

To install base-devel :

$ pacman -s base-devel

once the installation is done, go ahead and run the rustup installer, choose to manually install the rust prerequisites

[Pasted image 20231208125301.png]

A bunch of instructions and a prompt will show up, enter y for yes and hit enter

[Pasted image 20231208133030.png]

A set of options will show up next, select the one that says customize installation, after selecting customize installation, you'll be asked to enter a Default Host Triple.

This is where we define a connection to our mingW compiler instead of an MSVC compiler as the conventional installation will do.

inside the prompt enter:

x86_64-pc-windows-gnu

hit enter after this and an installation will start to run. While the installation is running, in your file system's explorer navigate to your cargo folder at C:\Users\your-username\.cargo

Create a file called .config and open it in your preferred editor. Inside the .config file paste this in the file:

[target.x86_64-pc-windows-gnu]
linker = "C:\\msys2\\mingw64\\bin\\gcc.exe"
ar = "C:\\msys2\\mingw64\\bin\\ar.exe"

When you're done, save and close the file.

After this, check if Rust is installed running rustc --version inside PowerShell

Windows Subsystem for Linux

If you're like me and prefer your entire developer environment to be in WSL, first of all, make sure WSL2 is set properly and updated using sudo apt-get update && sudo apt-get upgrade

After this, run the following inside your terminal, then follow the on-screen instructions to install Rust.


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

After this, check if Rust is installed by running rustc --version inside PowerShell

Updating Rust

If you for some reason installed an old version of Rust, you can upgrade your version of Rust using rustup by running rustup update inside your terminal.

Creating and Running a simple Rust program.

Create a new file anywhere on your computer called test.rs and launch it in your preferred code editor.

Inside the test.rs file, type this:

fn main() {
    println!("Hello World, This is a test!");
}

Once you've done this, in your command line, make you've navigated to the location of your rust file and compile the file using the rustc compiler

$ rustc test.rs

if there are no errors in your program, a test.exe file should be generated. Then you can run this in your command line to see the result of the program:

$ ./test

this should display our text in the terminal.

$ Hello World, This is a test!

Learning Resources: Where to go from here

Rust Foundation and the Rust community have a generous amount of learning resources to get started. Here are a few.

Tomes

The Official Rust Foundation

The interactive Rust Book by Brown University

Rust by example

Courses and Videos

Duke University Rust Course

FreeCodeCamp Rust Course

Google Rust Embedded Programming Course

Microsoft Rust Course

Rust Course For Web Development

Most of these are free but if you're looking for something paid(especially if you're coming from JavaScript):

Rust For JavaScript Developers