├── Cargo.toml ├── Cargo.lock ├── README.md └── src └── main.rs /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "sandbox-gpt-session-rust-0" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [dependencies] 7 | -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "sandbox-gpt-session-rust-0" 7 | version = "0.1.0" 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Here I Learned About 2 | - basic structs in Rust 3 | - unit structs and their capabilities 4 | - tuple structs 5 | - enums and basic pattern matching 6 | - traits and default implementations 7 | -------------------------------------------------------------------------------- /src/main.rs: -------------------------------------------------------------------------------- 1 | use std::fmt::Debug; 2 | 3 | fn main() { 4 | using_classic_structs(); 5 | using_tuple_structs(); 6 | using_unit_structs(); 7 | using_file_modes(ReadOnly{}); 8 | using_file_modes(WriteOnly{}); 9 | using_singletons(); 10 | using_unit_types_with_traits(); 11 | using_enum_with_match_arm(Message::ChangeColor(222, 222, 201)); 12 | using_traits_to_describe(); 13 | } 14 | 15 | 16 | //======================================= 17 | // TYPES 18 | //======================================= 19 | 20 | // classic struct 21 | #[derive(Debug)] 22 | struct User { 23 | username: String, 24 | email: String, 25 | age: u32, 26 | active: bool, 27 | } 28 | 29 | // tuple struct 30 | #[derive(Debug)] 31 | struct Color(u8, u8, u8); 32 | 33 | // unit struct 34 | #[derive(Debug)] 35 | struct Marker; 36 | 37 | // unit structs as options 38 | #[derive(Debug)] 39 | struct ReadOnly; 40 | #[derive(Debug)] 41 | struct WriteOnly; 42 | 43 | // another classic struct 44 | struct Crayon { 45 | color: String, 46 | } 47 | 48 | // a struct which is used as a singleton 49 | struct Logger; 50 | impl Logger { 51 | fn log(&self, message: &str) { 52 | println!("Log: {}", message); 53 | } 54 | } 55 | 56 | // a unit type and a trait, we can implement the trait on the unit type 57 | struct DefaultBehaviour; 58 | 59 | trait Action { 60 | fn perform(&self); 61 | } 62 | 63 | impl Action for DefaultBehaviour { 64 | fn perform(&self) { 65 | println!("performing the default behaviour!"); 66 | } 67 | } 68 | 69 | // an enum which holds multiple values, each with their own unique set of data 70 | enum Message { 71 | Quit, 72 | Move { x: i32, y: i32 }, 73 | Write(String), 74 | ChangeColor(u8, u8, u8), 75 | } 76 | 77 | // a trait that can be implemented on a type to "describe" it 78 | trait Describe { 79 | fn describe(&self) -> String { 80 | String::from("this is an object with no specific description") 81 | } 82 | } 83 | 84 | struct Animal { 85 | name: String, 86 | } 87 | 88 | struct Vehicle { 89 | model: String, 90 | } 91 | 92 | struct Unknown; 93 | 94 | impl Describe for Animal { 95 | fn describe(&self) -> String { 96 | format!("This is an animal named: {}", self.name) 97 | } 98 | } 99 | 100 | impl Describe for Vehicle { 101 | fn describe(&self) -> String { 102 | format!("This is a vehicle with the model: {}", self.model) 103 | } 104 | } 105 | 106 | impl Describe for Unknown {} // uses the default implementation 107 | 108 | //======================================= 109 | // FUNCTIONS 110 | //======================================= 111 | 112 | // creating and printing a classic struct 113 | fn using_classic_structs() { 114 | let user = User { 115 | username: String::from("alice"), 116 | email: String::from("alice@gmail.com"), 117 | age: 30, 118 | active: true, 119 | }; 120 | println!("{:?}", user); 121 | } 122 | 123 | // creating and printing a tuple struct 124 | fn using_tuple_structs() { 125 | let color = Color(255, 0, 0); 126 | println!("{:?}", color); 127 | } 128 | 129 | // creating and pringing a unit struct 130 | fn using_unit_structs() { 131 | let marker = Marker{}; 132 | println!("{:?}", marker); 133 | } 134 | 135 | // using a unit type to set different 'modes' on a file 136 | // here, I learned how to constrain a generic type to only types with certain traits 137 | fn using_file_modes(_mode: T) { 138 | println!("{:?}", _mode); 139 | } 140 | 141 | // we can also use unit structs to define singletons 142 | fn using_singletons() { 143 | let logger = Logger{}; 144 | logger.log("I am using a singleton!"); 145 | } 146 | 147 | // we can use unit structs to allow us to apply a trait to a type which has no internal data 148 | fn using_unit_types_with_traits() { 149 | let action = DefaultBehaviour; 150 | action.perform(); 151 | } 152 | 153 | // using an enum with a match arm 154 | fn using_enum_with_match_arm(message: Message) { 155 | match message { 156 | Message::Quit => println!("quitting!"), 157 | Message::Move { x, y } => println!("Move to ({}, {})", x, y), 158 | Message::Write(text) => println!("Write message: {}", text), 159 | Message::ChangeColor(r, b, g) => println!("Change color to RBG({}, {}, {})", r, b, g), 160 | } 161 | } 162 | 163 | // using traits to describe a few types 164 | fn using_traits_to_describe() { 165 | let animal = Animal{ 166 | name: String::from("Tiger John"), 167 | }; 168 | println!("{}", animal.describe()); 169 | let car = Vehicle{ 170 | model: String::from("Honda"), 171 | }; 172 | println!("{}", car.describe()); 173 | let unknown = Unknown{}; 174 | println!("{}", unknown.describe()); 175 | } 176 | 177 | --------------------------------------------------------------------------------