Skip to content

Instantly share code, notes, and snippets.

@carols10cents
Last active October 28, 2023 07:57
Show Gist options
  • Save carols10cents/65f5744b9099eb1c3a6f to your computer and use it in GitHub Desktop.
Save carols10cents/65f5744b9099eb1c3a6f to your computer and use it in GitHub Desktop.
JavaScript to Rust Cheat Sheet

JavaScript to Rust Cheat Sheet

The goal of this is to have an easily-scannable reference for the most common syntax idioms in JavaScript and Rust so that programmers most comfortable with JavaScript can quickly get through the syntax differences and feel like they could read and write basic Rust programs.

What do you think? Does this meet its goal? If not, why not?

Variables

JavaScript:

var foo = 1;
var bar = "hi";
var something_that_varies = 2;
something_that_varies += 1;

Rust:

let foo = 1i;
let bar = "hi";
let mut something_that_varies = 2;
something_that_varies += 1;

Functions

JavaScript:

// Function definition
function do_something(some_argument) {
  return some_argument + 1;
}
// Function use
var results = do_something(3);

Rust:

// Function definition: takes an integer argument, returns an integer
fn do_something(some_argument: int) -> int {
    some_argument + 1 // no semicolon in implicit return statements
}

// Function use
let results = do_something(3);

Conditionals

JavaScript:

if(x == 3) {
  // ...
} else if(x == 0) {
  // ...
} else {
  // ...
}

Less commonly in JavaScript, it seems to me, is the switch statement (not exactly equivalent since case uses ===):

switch (x) {
  case 3:
    // ...
    break;
  case 0:
    // ...
    break;
  default:
    // ... 
    break;
}

Rust:

if x == 3 {
    // ...
} else if x == 0 {
    // ...
} else {
    // ...
}

More commonly used in Rust is pattern matching, which gives you other benefits:

match x {
    3 => {
        // ...
    },
    0 => {
        // ...
    },
    _ => {
        // ...
    }
}

Output to the screen

In JavaScript, this is usually done using output to the JavaScript console:

var x = 5;
console.log("x has the value " + x);

Rust:

let x = 5;
println!("x has the value {}", x);

Lists of variable size

I'm not saying the word "array" on purpose :) Rust does have arrays, but they're fixed size, so you probably want a vector :)

JavaScript:

var i = ["a", "b", "c"];
i.push("d");
console.log(i[1]); // outputs b

Rust:

let i = vec!["a", "b", "c"];
i.push("d");
println!("{}", i[1]); // outputs b

Iterating over the elements in a list

JavaScript:

var i = ["a", "b", "c"];
i.forEach(function(element, index, array) {
  console.log(element);
});

Rust:

let i = vec!["a", "b", "c"];
for j in i.iter() {
    println!("{}", j);
}
@muminoff
Copy link

This is very useful resource for whom who came to Rust from Javascript. But I have to mention, that you have skipped the main features (generics/traits/borrowing/references) of Rust here since those can be implemented in Javascript using classes. I am also Rust beginner. Have already noted myself this kind of comparison with Python, but then I realized I have missed the main parts which cannot be "implemented" well in Python. Thanks for sharing.

@dperussina
Copy link

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment