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
// Simplest Example
let team_size = 7;
if team_size < 5 {
println!("Small");
} else if team_size < 10 {
println!("Medium");
} else {
println!("Large");
}
let tshirt_width = 20;
let tshirt_size = match tshirt_width {
16 => "S", // check 16
17 | 18 => "M", // check 17 and 18
19 ... 21 => "L", // check from 19 to 21 (19,20,21)
22 => "XL",
_ => "Not Available",
};
println!("{}", tshirt_size); // L
let mut a = 1;
while a <= 10 {
println!("Current value : {}", a);
a += 1; //no ++ or -- in Rust
}
// Usage of break and continue
let mut b = 0;
while b < 5 {
if b == 0 {
loop {
println!("Loop forever!");
}
// Usage of break and continue
let mut a = 0;
loop {
if a == 0 {
println!("Skip Value : {}", a);
a += 1;
for a in 0..10 { //(a = 0; a <10; a++) // 0 to 10(exclusive)
println!("Current value : {}", a);
}
// Usage of break and continue
for b in 0..6 {
if b == 0 {
println!("Skip Value : {}", b);
continue;
} else if b == 2 {
/// Line comments; document the next item
/** Block comments; document the next item */
//! Line comments; document the enclosing item
/*! Block comments; document the enclosing item !*/
/// This module contains tests
mod test {
// ...
}
mod test {
//! This module contains tests
// ...
/// Foo
#[doc="Foo"]
//! Foo
#![doc="Foo"]
let a = 5;
let b = a + 1; //6
let c = a - 1; //4
let d = a * 2; //10
let e = a / 2; // ⭐️ 2 not 2.5
let f = a % 2; //1
let g = 5.0 / 2.0; //2.5
let a = 1;
let b = 2;
let c = a == b; //false
let d = a != b; //true
let e = a < b; //true
let f = a > b; //false
let g = a <= a; //true
let h = a >= a; //true