Last active
March 3, 2020 06:01
-
-
Save iamsonal/b01b7498aa32cec84dbb3a0d943919c3 to your computer and use it in GitHub Desktop.
Rust Syntax
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 i: i32 = 255; | |
| println!("{}", i); | |
| // Tuples | |
| let tup = (1, 'c', true); | |
| println!("{} {}", tup.0, tup.1); | |
| println!("{:?}", tup); | |
| // Destructuring | |
| let (x, y, z) = tup; | |
| println!("{} {} {}", x, y, z); | |
| let a = [3, 5, 6]; | |
| println!("{}", a[0]); | |
| // Slices | |
| let b = &a[0..2]; | |
| println!("{:?}", b); | |
| } | |
| // Functions | |
| fn next_birthday(name: &str, current_age: u8) { // here name is the string slice | |
| let next_age = current_age + 1; | |
| println!( | |
| "Hi {}, on your next birthday, you'll be {}!", | |
| name, next_age | |
| ); | |
| } | |
| fn main() { | |
| next_birthday("Jake", 33); | |
| next_birthday("Vivian", 0); | |
| } | |
| // Returning Functions | |
| fn square(num: i32) -> i32 { | |
| num * num | |
| } | |
| fn main() { | |
| println!("The answer is {}", square(3)); | |
| } | |
| ================================================================================================================================== | |
| // Loops | |
| use std::io; | |
| fn main() { | |
| loop { | |
| println!("What's the secret word?"); | |
| let mut word = String::new(); | |
| io::stdin().read_line(&mut word).expect("Failed to read line"); | |
| if word.trim() == "rust" { | |
| break; | |
| } | |
| } | |
| println!("You know the secret word! Please proceed!"); | |
| } | |
| // While loop | |
| use std::io; | |
| fn main() { | |
| let mut word = String::new(); | |
| while word.trim() != "rust" { | |
| println!("What's the secret word?"); | |
| word = String::new(); | |
| io::stdin().read_line(&mut word).expect("Failed to read line"); | |
| } | |
| println!("You know the secret word! Please proceed!"); | |
| } | |
| // For loop | |
| fn main() { | |
| for i in 1..11 { | |
| println!("Now serving number {}", i); | |
| } | |
| } | |
| ================================================================================================================================== | |
| // Match expression - Pattern matching | |
| fn main() { | |
| let die1 = 1; | |
| let die2 = 5; | |
| match (die1, die2) { | |
| (1, 1) => println!("Snake eyes! Go back to the beginning."), | |
| (5, _) | (_, 5) => { | |
| println!("You rolled at least one 5!"); | |
| println!("Move and then roll again!"); | |
| }, | |
| _ => println!("Move your piece!"), | |
| } | |
| } | |
| // Match expression - Checking exhaustiveness | |
| fn main() { | |
| let is_confirmed = true; | |
| let is_active = false; | |
| match (is_confirmed, is_active) { | |
| (true, true) => println!("Your account is in good standing."), | |
| (false, true) => println!("You need to confirm your account!"), | |
| (false, false) => println!("This account will be deactivated."), | |
| _ => {} // removing the current line will give an error | |
| } | |
| } | |
| ================================================================================================================================== | |
| // Enums (check how enums are used. Also note the signature of next_player function | |
| enum HockeyPosition { | |
| Center, | |
| Wing, | |
| Defense, | |
| Goalie, | |
| } | |
| fn next_player(position: HockeyPosition) { | |
| // code that would do something like look up | |
| // another player at the position specified | |
| } | |
| fn main() { | |
| let position = HockeyPosition::Defense; | |
| next_player(position); | |
| } | |
| // Example of using enums and match expression | |
| enum Clock { | |
| Sundial(u8), | |
| Digital(u8, u8), | |
| Analog(u8, u8, u8), | |
| } | |
| fn tell_time(clock: Clock) { | |
| match clock { | |
| Clock::Sundial(hours) => | |
| println!("It is about {} o'clock", hours), | |
| Clock::Analog(hours, minutes, seconds) => { | |
| println!( | |
| "It is {} minutes and {} seconds past {} o'clock", | |
| minutes, seconds, hours, | |
| ); | |
| }, | |
| Clock::Digital(hours, minutes) => | |
| println!("It is {} minutes past {}", minutes, hours), | |
| } | |
| } | |
| fn main() { | |
| tell_time(Clock::Analog(9, 25, 45)); | |
| } | |
| ================================================================================================================================== | |
| // Structs | |
| enum HockeyPosition { | |
| Center, | |
| Wing, | |
| Defense, | |
| Goalie, | |
| } | |
| struct HockeyPlayer { | |
| name: String, | |
| number: u8, | |
| position: HockeyPosition, | |
| goals_ytd: u8, | |
| } | |
| fn main() { | |
| let mut player = HockeyPlayer { | |
| name: String::from("Bryan Rust"), | |
| number: 17, | |
| position: HockeyPosition::Wing, | |
| goals_ytd: 7, | |
| }; | |
| player.goals_ytd += 1; | |
| println!("{} has scored {} goals this season", | |
| player.name, | |
| player.goals_ytd, | |
| ); | |
| } | |
| // Tuple structs are structs that have a name for the whole type but don't name their fields | |
| struct Triangle(u32, u32, u32); | |
| fn is_equilateral(triangle: Triangle) -> bool { | |
| triangle.0 == triangle.1 && triangle.1 == triangle.2 | |
| } | |
| fn main() { | |
| let triangle1 = Triangle(3, 4, 5); | |
| is_equilateral(triangle1); | |
| // Can't pass a plain tuple to this function. Giving the tuple a name | |
| // by using tuple struct has made this a new type incompatible with other tuples | |
| //let nums = (5, 5, 5); | |
| //is_equilateral(nums); | |
| } | |
| // Struct without fields are called unit structs. We can define methods on them. | |
| struct MyStruct; | |
| fn main() { | |
| let s = MyStruct; | |
| } | |
| // Enum variants looking like structs | |
| enum Clock { | |
| Sundial { hours: u8 }, | |
| Digital { hours: u8, minutes: u8 }, | |
| Analog { hours: u8, minutes: u8, seconds: u8 }, | |
| } | |
| fn main() { | |
| let clock = Clock::Analog { | |
| hours: 9, | |
| minutes: 25, | |
| seconds: 46, | |
| }; | |
| } | |
| ================================================================================================================================== | |
| //Defining a method | |
| enum HockeyPosition { | |
| Center, | |
| Wing, | |
| Defense, | |
| Goalie, | |
| } | |
| struct HockeyPlayer { | |
| name: String, | |
| number: u8, | |
| position: HockeyPosition, | |
| goals_ytd: u8, | |
| } | |
| // the 1st parameter of a method is always a form of self | |
| impl HockeyPlayer { | |
| fn shoot_puck(self, seconds_remaining: u16) { | |
| if seconds_remaining < 300 { | |
| match self.position { // wherever you used hockey_player parameter, you use self | |
| HockeyPosition::Center => println!("Goal!"), | |
| _ => println!("Miss!"), | |
| } | |
| } else { | |
| println!("Goal!"); | |
| } | |
| } | |
| } | |
| fn main() { | |
| let mut player = HockeyPlayer { | |
| name: String::from("Bryan Rust"), | |
| number: 17, | |
| position: HockeyPosition::Wing, | |
| goals_ytd: 7, | |
| }; | |
| player.shoot_puck(1000); | |
| } | |
| // Associated functions: commonly used to create instances, so they don't have self instance to operate on | |
| enum HockeyPosition { | |
| Center, | |
| Wing, | |
| Defense, | |
| Goalie, | |
| } | |
| struct HockeyPlayer { | |
| name: String, | |
| number: u8, | |
| position: HockeyPosition, | |
| goals_ytd: u8, | |
| } | |
| // Note the use of new keyword | |
| impl HockeyPlayer { | |
| fn new(name: String, number: u8, position: HockeyPosition) -> HockeyPlayer { | |
| HockeyPlayer { | |
| name: name, | |
| number: number, | |
| position: position, | |
| goals_ytd: 0, | |
| } | |
| } | |
| } | |
| fn main() { | |
| let mut player = HockeyPlayer::new( // Check the use of new | |
| String::from("Bryan Rust"), | |
| 17, | |
| HockeyPosition::Wing, | |
| ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment