Lets Rust Up!

Sachin NairSachin Nair
4 min read

Getting started with Rust

Hey there, future Rustacean! Yess, you heard it right...we have an official title name for the rust developers, and it sound cool too! Ready to dive into the world of Rust? Let's get you set up and writing your first "Hello, World!" program in no time.

Why Rust

Rust has been winning hearts for the past four years, and there's a saying that perfectly captures its essence: "If your program compiles, it will most likely work and do what it's supposed to do." That's a game-changer, especially if you've ever grappled with the memory and pointer woes of C or C++.

Unlike those languages, Rust doesn't let you run wild with memory, sparing you from the headaches of runtime errors and debugging nightmares. But fear not! It still gives you the fine-grained control over memory that makes C and C++ so efficient and fast.

Think of Rust as the perfect blend—it lets you wield power over memory without plunging into the abyss of runtime errors. And trust me, that's not just me saying it; that's the essence of Rust itself, and it does this through its unique concepts of memory management which we will delve into later. Apart from these following can be termed as some of Rust's unique features -

Ownership and Borrowing
Concurrency Without Data Races
Pattern Matching
Traits and Generics
Fearless Concurrency
Zero-Cost Abstractions
Cargo and
Crates.io
Community and Documentation

A detailed note on these will be for some other blog, but for now, lets get our hands rusty already!

Setting Up Your Environment

In this blog, we will be delving into how to get the system setup done for getting started in Rust. First things first, let’s get Rust and Cargo installed on your machine. Since I'm on Windows and using Visual Studio Code, I’ll focus on that, but I’ll also give a nod to our friends on other OSes.

Install Rust and Cargo

To get Rust up and running, we’ll use Rustup, the Rust toolchain installer. Pretty easy!

  1. Open PowerShell (run as Administrator if it does not work with normal user).

  2. Enter the following command and hit enter.

     curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
    
  3. Follow the on-screen instructions. Rustup will handle the rest, and soon you’ll have Rust and Cargo (Rust’s package manager, just like npm is for Node.js, or Maven in Springboot) installed.

Verify installation

Once Rustup has done its magic, let’s make sure everything’s in place.

  1. Open a new PowerShell window.

  2. Type

     rustc --version
    

If you see the version number(something like this above), congratulations, you’ve got Rust!

Set Up Your IDE or Text Editor

Since you’re rocking Visual Studio Code, let’s get it set up for Rust:

  1. Open Visual Studio Code.

  2. Go to the Extensions view by clicking on the square icon in the sidebar or pressing Ctrl+Shift+X.

  3. Search for "Rust" and install the “rust-analyzer” extension. This gives you awesome features like code completion and inline error messages.

For those on MacOS or Linux, the installation steps are pretty similar. Just open your terminal and run the same Rustup command. And yes, Visual Studio Code works beautifully across all these platforms.

"Hello World!" from Rust

Now, let’s get our hands dirty with some actual coding.

  1. Open Visual Studio Code.

  2. Open the terminal in VS Code (`Ctrl+``).

  3. Create a new directory for your project:

     mkdir hello_rust
     cd hello_rust
    
  4. Initialize a new Rust project

     cargo new hello_world
     cd hello_world
    

Your project directory should look something like this - src folder, target folder, Cargo.lock and Cargo.toml files.

Now, let’s write the classic "Hello, World!" program. Open the main.rs file located in src directory. Replace its contents with:

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

To run your program, simply type:

cargo run

The above command does the following steps -

  1. Build the Project:
  • cargo run first compiles your Rust project. It goes through your source code, translates it into machine code, and produces an executable binary. This step ensures that any changes you've made to your code are included in the compiled program. The executable is then saved in the target folder in your Rust project directory structure.
  1. Runs the Executable:
  • After building the project, Cargo automatically runs the resulting executable. This saves you from having to manually find and execute the binary file.


So what did we do?

We’ve installed Rust, set up our development environment, and written our first Rust program. Rust is powerful, safe, and a joy to work with(if joy means you being irritated with compilation errors, sure.....jk!). Keep exploring, keep coding, and welcome to the Rustacean community!

Ready to dive deeper? Stick around, and we’ll explore more of Rust’s superpowers in the next posts.

0
Subscribe to my newsletter

Read articles from Sachin Nair directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sachin Nair
Sachin Nair

A computer science enthusiast, pursuing his Masters from IIIT Bangalore