Skip to content

Instantly share code, notes, and snippets.

@frizadiga
Last active April 25, 2020 13:48
Show Gist options
  • Select an option

  • Save frizadiga/6f890168a259207ffb1876d2f7096cd5 to your computer and use it in GitHub Desktop.

Select an option

Save frizadiga/6f890168a259207ffb1876d2f7096cd5 to your computer and use it in GitHub Desktop.
rust play
fn main() {
let text: String = String::from("Hi Word!");
println!("output: {}", text);
}
async {
let mut buf = [0; 1024];
let mut cursor = 0;
while cursor < 1024 {
cursor += socket.read(&mut buf[cursor..]).await?;
};
buf
}
/* Essential */
fn main() {
let n: i32 = 10;
for i in (0..n).step_by(1) {
println!("i: {}", i);
}
let result = n;
println!("result: {:?}", result);
}
/* Vector */
fn main() {
let nums = vec![1, 2, 3];
for i in nums {
println!("i: {}", i);
}
}
fn main() {
let mut n = 0;
loop {
if n > 10 { break; }
println!("cur n: {}", n);
n += 1;
};
let result = n;
println!("result: {:?}", result);
}
fn main() {
let age = 5;
let text;
match age {
a if a <= 5 => text = "balita".to_string(),
20 => text = "under".to_string(),
22 => text = "above".to_string(),
40..=50 => text = "tuwir".to_string(),
_ => text = "unset".to_string(),
}
println!("result: {}", text);
}
// Usage as namespace to hold methods
struct Calculator;
impl Calculator {
fn div (n1: i32, n2: i32) -> f32 {
(n1 / n2) as f32
}
}
fn main() {
let result = Calculator::div(30, 3);
println!("result {}", result);
}
// Common Usage
struct Data {
// str1: String;
num1: i32,
num2: i32,
}
impl Data {
fn sum(&self) -> i32 {
self.num1 + self.num2
}
}
fn main() {
let a = Data{
num1: 30,
num2: 50,
};
let result = Data::sum(&a);
println!("result {}", result);
}
struct Bike {
name: String,
power: u32,
}
impl Bike {
fn handle_say(self: &Bike) {
print!("Name is {}, Power is {} HP", self.name, self.power);
}
}
fn main() {
let bike = Bike { name: String::from("GSX"), power: 19 };
bike.handle_say();
}
fn main() {
let mut n: i32 = 0;
while n < 10 {
println!("n: {}", n);
n += 1;
}
let result = n;
println!("result: {:?}", result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment