issue160:c_c
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue160:c_c [2020/08/29 18:32] – créée auntiee | issue160:c_c [2020/09/03 11:37] (Version actuelle) – auntiee | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | Okay rustafarians, | + | **Okay rustafarians, |
- | 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? | + | 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: | + | OK, bandes de RUST-iques, nous revenons au développement de Rust car @Daredevil14 et Ellin se sont plaints de la direction que prenait le blog de Lucas sur Rust. Il semblerait que vous aimeriez toujours plus de rust et moins de « non-sens sur la sécurité ». En premier lieu, ne négligez pas la lecture du livre indiqué le mois dernier. Si vous êtes complètement débutant dans Rust ou même en programmation, |
+ | |||
+ | Pour le moment, je ne vous donne qu'une vision générale des choses. Nous pouvons rentrer dans le détail de chacun des sujets dont nous avons parlé. En général, quand nous voulons définir des types personnalisés, | ||
+ | |||
+ | **This will be chapter five in the book above. This is the direct link: | ||
https:// | https:// | ||
- | 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:// | + | 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:// |
+ | |||
+ | C'est dans le chapitre cinq du livre ci-dessus. Voici le lien direct : https:// | ||
+ | |||
+ | Les structs regroupent des choses, comme les statistiques sur vos personnages dans un jeu RPG (jeu de rôles). Aussi, disons que nous créons un personnage dans notre jeu de rôle. Les personnages ont un nom, ils possèdent des attributs « force », « dextérité », etc. Ces « caractéristiques » sont les mêmes pour tous les joueurs, que ce soit un demi-elfe ou un ogre. Si vous n' | ||
- | Like other things in Rust, we let Rust know what we are defining, so we use the struct keyword. | + | **Like other things in Rust, we let Rust know what we are defining, so we use the struct keyword. |
struct PlayerCharacter | struct PlayerCharacter | ||
Ligne 21: | Ligne 29: | ||
} | } | ||
- | 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. | + | 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). | + | Comme d' |
+ | |||
+ | struct PlayerCharacter | ||
+ | { | ||
+ | name: String, | ||
+ | gender: String, | ||
+ | race: String, | ||
+ | Strength: u8, | ||
+ | Agility: u8, | ||
+ | Constitution: | ||
+ | Charisma: u8, | ||
+ | } | ||
+ | |||
+ | Ce qui s' | ||
+ | |||
+ | **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: | 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:// | https:// | ||
- | The important take-away here is the how. How to use a struct. We can step through the code: | + | The important take-away here is the how. How to use a struct. We can step through the code:** |
+ | Ainsi, nous définissons la feuille de création du personnage, mais comment l' | ||
+ | Vous avez peut-être noté un point-virgule bizarrement placé après la définition du joueur. Moi aussi ; pourquoi est-il là ? Je dis que c'est parce que c'est une déclaration d' | ||
- | We define our variable “player” as assigned to the struct name, “PlayerCharacter”. | + | Le point important ici est « comment ». Comment utiliser un struct. Nous pouvons passer le code en revue. |
+ | |||
+ | **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. | 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. | + | 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, | + | Nous définissons notre variable « player » (joueur) comme affectée au nom de struct « PlayerCharacter ». |
+ | |||
+ | Pour mémoire, les variables ne sont pas des variables dans Rust, à moins d' | ||
+ | |||
+ | Ensuite, nous remplissons tous les champs définis, que nous prévoyions de les utiliser immédiatement ou non. (Vous noterez que je ne les utilise pas et que j'ai eu des erreurs de compilation. Si vous poursuivez, vous pouvez les ignorer pour le moment et vous verrez la sortie désirée.) Nous terminons notre déclaration d' | ||
+ | |||
+ | **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, | ||
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. | 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:// | + | Okay I just watched this: https:// |
+ | |||
+ | Alors, qu' | ||
+ | |||
+ | Je dois insister sur le fait que les structs avec des champs nommés ne sont pas le seul type. Le premier est un struct de tuple. Dans un struct de tuple, les champs sont invariables ; ils n'ont donc pas besoin d' | ||
+ | |||
+ | Bon, d' | ||
- | We simply refer to the position of the tuple’s contents. | + | **We simply refer to the position of the tuple’s contents. |
A tuple struct is not the same type as a tuple! | A tuple struct is not the same type as a tuple! | ||
Ligne 52: | Ligne 91: | ||
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. | 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 | + | If I made a mistake, fix it and send it to: misc@fullcirclemagazine.org** |
+ | |||
+ | Nous faisons simplement référence à la position des contenus du tuple. | ||
+ | |||
+ | Un struct de tuple, ce n'est pas pareil qu'un tuple ! | ||
+ | |||
+ | Je n' | ||
+ | |||
+ | Un struct unit est un autre type. C'est un struct sans aucun champ. Je ne sais pas dire quel est son usage, mais nous pen parlerons dans le prochain numéro quand nous regarderons les enums. | ||
+ | |||
+ | Si j'ai fait une erreur, corrigez-la en m' |
issue160/c_c.1598718721.txt.gz · Dernière modification : 2020/08/29 18:32 de auntiee