Skip to content

Instantly share code, notes, and snippets.

View dumindu's full-sized avatar
🌱
One step at a time...

Dumindu Madunuwan dumindu

🌱
One step at a time...
View GitHub Profile
static N: i32 = 5;
let x = true;
let y: bool = false;
// ⭐️ no TRUE, FALSE, 1, 0
let x = 'x';
let y = '😎';
// ⭐️ no "x", only single quotes
//because of Unicode support, char is not a single byte, but four.
let a = [1, 2, 3]; // a[0] = 1, a[1] = 2, a[2] = 3
let mut b = [1, 2, 3];
let c: [int; 3] = [1, 2, 3]; //[Type; NO of elements]
let d: ["my value"; 3]; //["my value", "my value", "my value"];
let e: [i32; 0] = []; //empty array
println!("{:?}", a); //[1, 2, 3]
let a = (1, 1.5, true, 'a', "Hello, world!");
// a.0 = 1, a.1 = 1.5, a.2 = true, a.3 = 'a', a.4 = "Hello, world!"
let b: (i32, f64) = (1, 1.5);
let (c, d) = b; // c = 1, d = 1.5
let (e, _, _, _, f) = a; //e = 1, f = "Hello, world!", _ indicates not interested of that item
let g = (0,); //single-element tuple
let a: [i32; 4] = [1, 2, 3, 4];//Parent Array
let b: &[i32] = &a; //Slicing whole array
let c = &a[0..4]; // From 0th position to 4th(excluding)
let d = &a[..]; //Slicing whole array
let e = &a[1..3]; //[2, 3]
let e = &a[1..]; //[2, 3, 4]
let e = &a[..3]; //[1, 2, 3]
let a = "Hello, world."; //a: &'static str
let b: &str = "こんにちは, 世界!";
//Hello world function
fn main() {
println!("Hello, world!");
}
//Function with arguments
fn print_sum(a: i8, b: i8) {
println!("sum is: {}", a + b);
}
fn plus_one(a: i32) -> i32 {
a + 1
}
let b = plus_one;
let c = b(5); //6
let b: fn(i32) -> i32 = plus_one; //same, with type inference
let c = b(5); //6
// Line comments
/* Block comments */