This is an old revision of the document!
I spent the weekday mostly working and doing other things so I try to spend a couple hours every weekend doing more rustling challenges as it still seems thats getting me to understand the basics of rust better. After I finish rustlings I will go back to looking at bevy. Maybe it will look less cryptic.
Structs hold a bunch of values like tuples. But a struct will have each piece of data named ie: key:value pairs. Structs are pretty powerful as there are no Classes in rust. Structs are used to make “Classes”
struct Duck{ awesomeness:f32, //Basic values name:(String), color:(u8,u8,u8), //Tuples can be stored //I DONT THING YOU CAN NEST ENUMS..... lmk bois }
To use a struct, you can access bits of it using the dot notation.
let dave = Duck{ awesomeness:100.0, name: String::from("Dave"), color:(255,255,0), }; println!("{}",dave.name);
We (yes, both you and I) can make a new struct that has all the same values as another instance of a struct and then change whatever you want.
//Given the duck struct above and its instance of dave let david = { name: String::from("Daveed"), ..dave }
david is the same as dave except for his name! 1)
Structs can also have functions attached to it. using impl.
impl Duck{ fn print_name(){ println!("{}",self.name); } }
this is how you can make a class with structs. The Declaration of the struct is like a C++ header file and the impl like the cpp file 2).
YOU caaaaaan also deconstruct structs but I didnt havtadoit for the exercises so idkfa.
Enums are another grouping of data. One major difference between enums and structs is that with structs you MUST initialize every value when you instance it. With an enum you can only pick one of its possible values while instancing. Usually its used when you have different options and can pick one(variants). EG FROM THE BOOK
enum Message { ChangeColor(u8,u8,u8), Echo(String), Move{x:u8,y:u8}, Quit, }
This enum has for variants and each one hold a different type. This way our one message enum can deal with many use cases rather than having different overloaded functions.
We can use “matching” to go through every value (enumerate) and perform different actions based on the value matched.
let message = Message::Quit; match message{ Message::Move{x,y} => {self.move_position(Point{x,y});} Message::Echo(message) => {self.echo(message);} Message::ChangeColor(h,s,v) => {self.change_color((h,s,v));} Message::Quit => {self.quit();} }
EZ PZZ probabbly more nuacnces than that tho squeeqzy
F's
Vogue Baby Struct a pose