Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rictorres/487a0035a8f01124475fa1edbba157d9 to your computer and use it in GitHub Desktop.
Save rictorres/487a0035a8f01124475fa1edbba157d9 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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment