Skip to content
nive.sh

Loading index…

↑↓ navigate · ↵ opennive.sh

Hello, Rust!

Welcome to Rust for JavaScript Developers! It's fantastic to have you here. Let's dive right in.

Nivesh Saharan

· 3 min read · 505 words

Introduction

Welcome to Rust for JavaScript Developers! It's fantastic to have you here. Let's dive right in.

Installation

Let's install Rust. If you're on macOS, Linux, or another Unix-like OS, spin up a terminal and enter the following:

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

This will use rustup to install the latest stable version of Rust.

Once rustup is done doing its thing, run rustc --version to ensure you got everything you need and to see the version of Rust you just installed. rustc is the Rust compiler by the way.

Now, let's write our very first Rust program! Are you ready? Create a file, call it hello-rust.rs and open it in your favourite editor.

Type the following into your brand new hello-rust.rs file.

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

Now, time to compile it. Go back to the terminal and run:

rustc hello-rust.rs

Lo and behold, you'll now have a nice little compiled binary in the same directory called hello-rust which you can run by running:

./hello-rust

This should print out "Hello, Rust!" in your terminal. You did it!

Give yourself a pat on the back. You just wrote your first Rust program!

Now let's take a deeper look into that syntax.

Syntax

Functions

To create a function in Rust, you use the fn keyword. This is just like the function keyword in JavaScript. The rest is identical syntactically.

fn hola() {}

Printing

println! as the name suggests is how you print something (with a nice line in the end) to the screen. Much like our dear friend, console.log. One quick difference though. println! is not a function. It's a macro. We'll learn what those are later but that explains the exclamation mark. Macros are called just like functions but with the additional ! before the parenthesis.

String literals

String literals in Rust (just like JavaScript) live in double quotes. Unlike JavaScript though, you can't use ", " or ` interchangeably so you need to stick with a pair of ".

Semicolons

The final bit of relevant syntax in our Hello, Rust example is that little semicolon. Unlike JavaScript, semicolons in Rust are not optional. Every statement in Rust must end with a semicolon.

Semantics

main

Semantically, the only bit in our example we must talk about is the main function. Every Rust program needs to have a main function. That is the entry point to your program and is called when your binary is executed. If this were JavaScript, we would have to call it ourselves.

Keep reading

Related articles

All articles

Jun 27, 2023 · 5 min read

Arrays and Tuples

There are two primitive compound types in Rust: Arrays and Tuples.

  • JavaScript
  • Rust

Jun 23, 2023 · 3 min read

Scalars

Rust is a statically typed programming language. This means that all values must have a known type at compile time. So how did we get away without adding annotations in our Hello, Rust example? The Rust compiler is quite excellent at type inference and will often infer types for your values and only ask you to specify a type if it can't.

  • JavaScript
  • Rust

Jun 23, 2023 · 3 min read

Statements and Variables

Code is typically composed of statements. Statements consist of one or more values and operators. Initialising a variable, setting it to the return value of a function call, even calling a macro (like the println macro we saw a while ago) are all examples of statements.

  • JavaScript
  • Rust

Newsletter

Build notes, when there is something worth sending.

New deep-dive articles and the occasional note on what I am shipping. No schedule, no filler, unsubscribe any time.

Taking on new projects

Need help with custom web and mobile engineering?

Tell me about the storefront, product or platform you’re working on. A 30-minute call is usually enough to map the first steps.

Email

hey@nive.sh

Local time

--:-- · Gurugram (IST)