issue160:c_c
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
issue160:c_c [2020/08/31 09:28] – d52fr | issue160:c_c [2020/09/03 11:37] (Version actuelle) – auntiee | ||
---|---|---|---|
Ligne 2: | Ligne 2: | ||
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?** | ||
+ | |||
+ | 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: | **This will be chapter five in the book above. This is the direct link: | ||
Ligne 7: | Ligne 11: | ||
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. | ||
Ligne 22: | Ligne 30: | ||
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.** | ||
+ | |||
+ | 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). | **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). | ||
Ligne 30: | Ligne 53: | ||
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' | ||
+ | |||
+ | 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”. | **We define our variable “player” as assigned to the struct name, “PlayerCharacter”. | ||
Ligne 37: | Ligne 64: | ||
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. ** | ||
+ | |||
+ | 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, | **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, | ||
Ligne 43: | Ligne 76: | ||
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. | ||
Ligne 53: | Ligne 92: | ||
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.1598858937.txt.gz · Dernière modification : 2020/08/31 09:28 de d52fr