Gabriel Rufino
Gabriel Rufino's Blog

Gabriel Rufino's Blog

Translating TypeScript to Rust #2

Translating TypeScript to Rust #2

Gabriel Rufino's photo
Gabriel Rufino
·Mar 1, 2022·

2 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

Remember that every translation is subject to inaccuracies in some cases. Feel free to correct or suggest better translations in the comments.

2.1 - The if statement

TypeScript

const condition: boolean = true
if (condition) {
  console.log('Print me!')
}

Rust

let condition: bool = true;
if condition {
  println!("Print me!");
}

2.2 - The while statement

TypeScript

let counter = 0
while (counter <= 10) {
  console.log(counter)
  counter++
}

Rust

let mut counter = 0;
while counter <= 10 {
  println!("{}", counter);
  counter += 1;
}

2.3 - The for statement

TypeScript

for (let i = 0; i <= 10; i++) {
  console.log(i)
}

Rust

for i in 0..11 {
  println!("{}", i);
}
for i in 0..=10 {
  println!("{}", i);
}

2.4 - Comments

TypeScript

// Useless comment about n
const n: number = 10

/*
Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
*/

Rust

// Useless comment about n
let n: i32 = 10;

/*
Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
*/

2.5 - Infinite loop

TypeScript

while (true) {
  console.log('Forever!?')

  if (stopCondition) {
    break
  }
}

Rust

loop {
  println!("Forever!?");

  if stop_condition {
    break;
  }
}

Did you find this article valuable?

Support Gabriel Rufino by becoming a sponsor. Any amount is appreciated!

Learn more about Hashnode Sponsors
 
Share this