This is the first post of a series of many posts translating TypeScript into Rust language. The idea is pretty simple like this: help those who know TypeScript but it's learning Rust with two code snippets, one in TypeScript and one for Rust.
Remember that every translation is subject to inaccuracies in some cases. Feel free to correct or suggest better translations in the comments
1.1 - Output some text
TypeScript
console.log('Hello, world!')
Rust
println!("Hello, world!");
1.2 - Define a immutable variable
TypeScript
const PI = 3.1415
Rust
let PI = 3.1415;
1.3 - Define a mutable variable
TypeScript
let count = 0
Rust
let mut count = 0;
1.4 - Define variables with types
TypeScript
const n: number = 10
const f: number = 7.456
const s: string = 'Gabriel Rufino'
const b: boolean = true
Rust
let n1: i8 = 10;
let n2: i16 = -20;
let n3: i32 = 30;
let n4: i64 = -40;
let n5: u8 = 10;
let n6: u16 = 20;
let n7: u32 = 30;
let n8: u64 = 40;
let f1: f32 = 3.14;
let f2: f64 = 3.1415;
let s: String = String::from("Gabriel Rufino");
let b: bool = true;
1.5 - Define a sum function
TypeScript
function sum (x: number, y: number): number {
return x + y;
}
Rust
fn sum(x: i32, y: i32) -> i32 {
x + y
}