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
| // main.rs | |
| struct Counter { | |
| hits: u32, | |
| } | |
| trait Logger { | |
| // Log relevant message to standard output | |
| fn log(&self, message: String); |
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 Counter { | |
| hits: u32, | |
| } | |
| trait Logger { | |
| // Log relevant message to standard output | |
| fn log(&self, message: String); | |
| // Write some relevant information about self to log | |
| fn write_state_to_log(&self); |
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
| // enums2.rs | |
| use std::rand::Rng; | |
| use std::rand; | |
| enum CoordinateFormula { | |
| FirstIteration, | |
| SecondIteration, | |
| SubsequentIteration, | |
| } |
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 coordinate_formula = CoordinateFormula::new(); | |
| let (x1, y1) = coordinate_formula.calculate_coordinates(40); | |
| println!("x {0} y {1}", x1, y1); | |
| coordinate_formula = coordinate_formula.change_iteration(); | |
| let (x2, y2) = coordinate_formula.calculate_coordinates(40); | |
| println!("x {0} y {1}", x2, y2); | |
| coordinate_formula = coordinate_formula.change_iteration(); |
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
| use std::rand::Rng; | |
| use std::rand; | |
| enum CoordinateFormula { | |
| FirstIteration, | |
| SecondIteration, | |
| SubsequentIteration, | |
| } | |
| impl CoordinateFormula { |
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
| enum Iteration { | |
| Initial, | |
| NonInitial, | |
| } | |
| impl Iteration { | |
| fn new() -> Iteration { | |
| Initial | |
| } |
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 factorial(number: int) -> int { | |
| match number { | |
| 0 => 1, | |
| x => x * factorial(x - 1), | |
| } | |
| } | |
| fn fibonacci(number: int) -> int { | |
| match number { | |
| 0 => 1, |
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
| match number { | |
| 2 => println!("It's two!"), | |
| x if x < 10 => println!("lower than ten"), | |
| 11..12 => println!("eleven or twelve"), | |
| _ => println!("anythingelse"), | |
| } |
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
| let number = 5; | |
| match number { | |
| 1 => println!("1"), | |
| 2 => println!("2"), | |
| _ => println!("anythingelse"), | |
| } |
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
| // pattern_matching.rs | |
| fn main() { | |
| let number = 5; | |
| // basic 'switch statement like behaviour | |
| match number { | |
| 1 => println!("1"), | |
| 2 => println!("2"), | |
| _ => println!("anythingelse"), | |
| } |