Last active
October 28, 2023 07:57
-
-
Save carols10cents/65f5744b9099eb1c3a6f to your computer and use it in GitHub Desktop.
Revisions
-
carols10cents revised this gist
Dec 27, 2014 . 1 changed file with 0 additions and 61 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -163,64 +163,3 @@ for j in i.iter() { println!("{}", j); } ``` -
carols10cents created this gist
Dec 27, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,226 @@ # 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: ```js var foo = 1; var bar = "hi"; var something_that_varies = 2; something_that_varies += 1; ``` Rust: ```rust let foo = 1i; let bar = "hi"; let mut something_that_varies = 2; something_that_varies += 1; ``` ## Functions JavaScript: ```js // Function definition function do_something(some_argument) { return some_argument + 1; } // Function use var results = do_something(3); ``` Rust: ```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: ```js 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 `===`): ```js switch (x) { case 3: // ... break; case 0: // ... break; default: // ... break; } ``` Rust: ```rust if x == 3 { // ... } else if x == 0 { // ... } else { // ... } ``` More commonly used in Rust is pattern matching, which gives you other benefits: ```rust match x { 3 => { // ... }, 0 => { // ... }, _ => { // ... } } ``` ## Output to the screen In JavaScript, this is usually done using output to the JavaScript console: ```js var x = 5; console.log("x has the value " + x); ``` Rust: ```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: ```js var i = ["a", "b", "c"]; i.push("d"); console.log(i[1]); // outputs b ``` Rust: ```rust let i = vec!["a", "b", "c"]; i.push("d"); println!("{}", i[1]); // outputs b ``` ## Iterating over the elements in a list JavaScript: ```js var i = ["a", "b", "c"]; i.forEach(function(element, index, array) { console.log(element); }); ``` Rust: ```rust let i = vec!["a", "b", "c"]; for j in i.iter() { println!("{}", j); } ``` ## Tests Using the standard libraries. No, I am not translating every Ruby testing library for you. Ruby: ```ruby def some_test assert true # will pass assert_equal("expected", "actual") # will fail end ``` Rust: ```rust #[test] fn some() { assert!(true); // will pass assert_eq!("expected", "actual"); // will fail } ``` ## Encapsulation of data + behavior I'm not saying the word "object" on purpose :) Ruby: ```ruby class Highway def initialize(length, speed_limit) @length = length @speed_limit = speed_limit end def time_to_travel length / speed_limit end end Highway.new(325, 65).time_to_travel # returns 5 ``` Rust: ```rust struct Highway { length: int, speed_limit: int, } impl Highway { fn time_to_travel(&self) -> int { self.length / self.speed_limit } } Highway { length: 325, speed_limit: 65 }.time_to_travel() // returns 5 ```