Translating TypeScript to Rust #2

Translating TypeScript to Rust #2

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!