Rust for JavaScript Developers

Subscribe to my newsletter and never miss my upcoming articles

The Rust Langauge

Rust is a multi-paradigm programming language that focusses on performance and safety. The language has been around since 2010 and it has been the most loved language in the StackOverflow developer survey since 2016. You can do anything with Rust, like building operating systems, games, and web browsers. Deno the new JavaScript and Typescript runtime is built with Rust.

This post isn't meant to teach you Rust, it just compares Rust and JavaScript. But you'll learn something.

Coming from a JavaScript background every time I write something in Rust I think of how I can write that same thing in JavaScript and what the differences are.

You can run your Rust code here

1. Printing to the Terminal

Rust

fn main(){
   println!("Hello there");
   print!("Hello there again");
}

JavaScript

console.log("Can you see me");

Sure you can tell from the above code snippets what the results will be, but let's discuss the Rust code. Unlike JavaScript, every Rust program needs a main function to run, that's why there's fn main(). The fn keyword declares a function named main. The println! and print! are both used to print to the terminal but println! appends a new line after printing.

2. Declaring a Variable

Rust

fn  main(){
  let name = "Abimbola";
  print!("My name is {}", name); // My name is Abimbola
}

JavaScript

let name = "Abimbola";
console.log(`My name is ${name}`);// My name is Abimbola

First of all, know that Rust variables are immutable by default, meaning, you cannot reassign a value to them, unlike JavaScript. let is the identifier, name is the variable name and Abimbola the value. Now let's talk about {}, it's referred to as a placeholder it can only hold a value or expression at a time since we can't directly input the variable inside the quote " ". You can make Rust variables mutable by adding mut to the identifier

fn main(){
   let mut x = "Rust is beautiful";
   println!("{}", x); // Rust is beautiful
   x = " Vrooooom";
   println!("{}",x); // Vrooooom
}

Also declaring a constant has the same identifier with JavaScript, the keyword const is used. All characters are usually in uppercase.

fn main(){
   const LANGUAGE = "Rust";
   print!("{}",LANGUAGE); //Rust
}

3. Assigning Multiple Value

Rust

 fn main(){
    let (name, language) = ("Abimbola", "Rust");
    print!("My name is {} I love {}", name, language); // My name is Abimbola I love Rust
}

JavaScript

let [name, language] = ["Abimbola", "JavaScript"]
console.log(`My name is ${name} I love ${language}`); // My name Abimbola I love JavaScript

Both examples show how to assign multiple variables. JavaScript unpacks from an Array while Rust unpack from a Tuple.

4. Ternary Operator

Rust

fn main(){
 let age = 17;
 if age >= 18{
    print!(" You are old enough to vote");
} else {
    print!("Come back when you are 18");
    }
}

//Ternary Operator
fn main(){
let age = 17;
if age >= 18 {print!(" You are old enough to vote")} else {print!("Come back when you are 18")}
}

JavaScript

let age = 17 
if(age >= 18){
    console.log(" You are old enough to vote");
} else {
   console.log("Come back when you are 18");
}

// Ternary Operator 
age >= 18 ? console.log(" You are old enough to vote") : console.log("Come back when you are 18");

Writing ternary operator or short hand if/else in Rust is a little bit similar to JavaScript.

5. Match or Switch for JS

Rust

fn main(){
   let language = "Rust";
   match language{
   "JavaScript" => print!("JavaScript: The Good Part"),
   "Rust" => print!("The Rust Langauge"),
   "C++" => print!("C++ for dummies"),
    _ => print!("Choose something dummy")
  } // The Rust langauge
}

JavaScript

let language = "JavaScript";
switch language{
   case "JavaScript":
         console.log("JavaScript: The Good Part");
         break;
   case "Rust":
        console.log("The Rust Language");
        break;
   case "C++":
        console.log("C++ for dummies");
        break;
   default:
        console.log("Choose something dummy");
} // JavaScript: The Good Part

Match expression just like Switch checks if the current value correspond to any value within the list of values. If just like if/else statement. While default is used when a match can't be found in JavaScript, _ is used in Rust.

If you want to learn more about Rust you can check the Learn Rust from Scratch on Educative. Thanks for reading. You can connect with me on Twitter @bimbooladele_

fengxh's photo

I'm sorry, but there is an error in javascript code about ternary operator

let age = 17 
if(age >= 18){
    console.log(" You are old enough to vote");
}else{
   console.log("Come back when you are 18");
}

// Ternary Operator 
if age >= 18 ? console.log(" You are old enough to vote") : console.log("Come back when you are 18");
// here we don't need if identifier, just remove it
Show +2 replies
Abimbola Oladele's photo

Software Developer

Yes they are not ternary operators, it's just the closet thing I could compare with JavaScript syntax. Thanks for pointing it out. j

j's photo

stuff ;)

Abimbola Oladele I know, that's why I didn't mention it on the first read, but since some one else pointed it out. Also match is not a switch but ... for the sake of a javascript perspective you could use it that way :D true.

anyhow good article, I don't want to sidetrack to much :)

Muhammad Sahil's photo

Nice comparisons Really liked it