-
-
Save frizadiga/6f890168a259207ffb1876d2f7096cd5 to your computer and use it in GitHub Desktop.
rust play
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fn main() { | |
| let text: String = String::from("Hi Word!"); | |
| println!("output: {}", text); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| async { | |
| let mut buf = [0; 1024]; | |
| let mut cursor = 0; | |
| while cursor < 1024 { | |
| cursor += socket.read(&mut buf[cursor..]).await?; | |
| }; | |
| buf | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* 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); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fn main() { | |
| let mut n = 0; | |
| loop { | |
| if n > 10 { break; } | |
| println!("cur n: {}", n); | |
| n += 1; | |
| }; | |
| let result = n; | |
| println!("result: {:?}", result); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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