Ceci est une ancienne révision du document !
Okay rustafarians, we head back to Rust development – @Daredevil14 and Ellin complained about where Lucas’ rust blog went. It looks like you guys want more rust and less ‘security nonsense’. Firstly, do not neglect to read the above book. If you are a complete beginner to rust, or even programming, we got you covered here at FCM. These articles are not to replace the book mentioned above, think of it more as helpers along the way.
At the moment, we are just taking a high-level view of things. We can drill down into each of these topics we have covered in more detail. Usually when you want to define custom types, it’s ‘structs’ and ‘enums’. Let’s start with structs?
This will be chapter five in the book above. This is the direct link: https://doc.rust-lang.org/book/ch05-01-defining-structs.html
Structs group things together, like your character stats in a RPG game. So let’s say we create a character in our RPG. Characters have a name, they have a strength attribute, a dexterity attribute, etc. These “characteristics” are the same across any player, be it a half elf or an ogre. If you are not familiar with RPG games or DND, quickly look up DND (or D&D) character creation on the interwebs. I did not like the example here: https://doc.rust-lang.org/rust-by-example/custom_types/structs.html. But, if it makes more sense to you than my example, please go ahead on that page.
Like other things in Rust, we let Rust know what we are defining, so we use the struct keyword.
struct PlayerCharacter {
name: String, gender: String, race: String, Strength: u8, Agility: u8, Constitution: u8, Charisma: u8,
}
What you are looking at is a CSV (Comma Separated Values) just neatly below each other. You can think of them as fields in an excel or calc worksheet.
So we define our struct character creation sheet, but how do we actually use it? Well, to use it, you need to remember to use all the fields that you created. So for demonstration purposes, I am not going to type all those fields we listed above, but a subsection of them. We will encounter an error, but the program will run. (I do not use all the fields, that’s all).
You may have noticed an odd semicolon after the player definition. I was like, why is that there? I say it is because it is an assignment statement, even if it doesn’t look that way at first. However, here is more opinions: https://users.rust-lang.org/t/why-is-a-semicolon-required-at-the-end-of-a-tuple-struct-definition/25589
The important take-away here is the how. How to use a struct. We can step through the code:
We define our variable “player” as assigned to the struct name, “PlayerCharacter”.
Just a reminder, variables are not variable in Rust unless you use the mut keyword. So if you plan on changing these variables, say strength or agility as your character levels up, you need mut after let.
We then fill all the defined fields, if we plan on using them immediately or not. (You will notice that I did not use them and I got compiler errors. If you follow along, you can ignore these for now and you will see the desired output). We end our assignment statement with a semicolon. Think of it as a struct instance.
So what is happening in the println? When you reference the field you want, you need “variablename.fieldname” . The reason for this is that you may have multiple PlayerCharacters, and referring to each may be player1.name, player2.name (instead of my lame character.name). See where this is going?
I have to point out that structs with named fields are not the only type. The first is a tuple struct. In a tuple struct the fields are immutable, so they do not need naming.
Okay I just watched this: https://www.youtube.com/watch?v=wh4aWZRtTwU - this morning, so don’t judge my example. It is actually HARD coming up with simple examples! (top right)
We simply refer to the position of the tuple’s contents.
A tuple struct is not the same type as a tuple!
I can’t imagine a real-world use case, but my day job is not a programmer, so if anyone wants to jump in here, please feel free!!!
A unit struct is the other type. This is a struct without any fields. I cannot say what use they would be, but we can discuss that in the next issue when we look at enums.
If I made a mistake, fix it and send it to: misc@fullcirclemagazine.org