Outils pour utilisateurs

Outils du site


issue158:c_c

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentesRévision précédente
Prochaine révision
Révision précédente
issue158:c_c [2020/07/01 14:02] auntieeissue158:c_c [2020/07/03 14:45] (Version actuelle) andre_domenech
Ligne 1: Ligne 1:
-Okay, rustafarians, we head back to Rust development, as @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. +**Okay, rustafarians, we head back to Rust development, as @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.
  
 Defining your own functions: Defining your own functions:
  
-Why? Functions are just code blocks you can use over and over by calling them. Clearly, they simplify life. Functions can return a value, but they do not have to. You have seen a function, the main function we have been playing with. It is good practice to write functions, then simply call them in the main function. Rule of thumb, you recognise a function by the parenthesis. Example: Println!();+Why? Functions are just code blocks you can use over and over by calling them. Clearly, they simplify life. Functions can return a value, but they do not have to. You have seen a function, the main function we have been playing with. It is good practice to write functions, then simply call them in the main function. Rule of thumb, you recognise a function by the parenthesis. Example: Println!();** 
 + 
 +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, vous trouverez ce qu'il vous faut ici, dans le FCM. Ces articles n'ont pas l'intention de remplacer le livre cité plus haut ; voyez-les plutôt comme une aide en parallèle. 
 + 
 +Définissez vos propres fonctions : 
 + 
 +Pourquoi ? Les fonctions ne sont que des blocs de code que vous utilisez encore et encore en les appelant. Clairement, elles vous simplifient la vie. Les fonctions peuvent vous renvoyer une valeur, mais ce n'est pas une obligation.  Vous avez vu une fonction, la fonction principale avec laquelle nous avons joué. C'est une bonne pratique d'écrire des fonctions, puis de simplement les appeler dans la fonction principale. Astuce : vous reconnaissez une fonction à ses parenthèses. Exemple : Println!();
    
-In C, one specifies the type of return value before the function. +**In C, one specifies the type of return value before the function. 
  
 int main (void) int main (void)
Ligne 22: Ligne 28:
 } }
  
-Now I need you guys with peeled peepers. What is different? I’ll wait.......+Now I need you guys with peeled peepers. What is different? I’ll wait.......**
  
-If you picked up that there was no semicolon at the end of the statement inside the adder function, you were correct. This confuses the heck out of newbies. But, there is a syntax rule. You want to return something (a value is expected), that something comes from the LAST line. (Think of the semicolon as suppressing the output). In that case, you do not use a semicolon at the end of the statement. (The ‘last line’ is our only line).+En C, on spécifie le type de la valeur retournée avant la fonction. 
 + 
 +int main (void) 
 + 
 +Dans Rust, une flèche est utilisée. Pour faire une flèche, saisissez juste un tiret suivi du symbole « plus grand que », ->. 
 + 
 +fn <nomdefonction>(paramètres) -> typeretourné {} 
 + 
 +Ça peut paraître un peu bizarre... Au moins, ça l'est pour moi. 
 + 
 +Pour simplifier, disons que nous voulons faire une fonction qui additionne deux chiffres et retourne le résultat. Elle pourrait ressembler à quelque chose comme ceci : 
 + 
 +fn adder (num1: i32, num2: i32) -> i32 
 +   { 
 +   num1 + num2 
 +   } 
 + 
 +Maintenant, j'ai besoin que vous ouvriez bien vos mirettes. Qu'y a-t-il de différent ? J'attends... 
 + 
 +**If you picked up that there was no semicolon at the end of the statement inside the adder function, you were correct. This confuses the heck out of newbies. But, there is a syntax rule. You want to return something (a value is expected), that something comes from the LAST line. (Think of the semicolon as suppressing the output). In that case, you do not use a semicolon at the end of the statement. (The ‘last line’ is our only line).
  
 If this is unclear or you have a better way to explain it, please email us on:  If this is unclear or you have a better way to explain it, please email us on: 
 misc@fullcirclemagazine.org  misc@fullcirclemagazine.org 
  
-Now I actually want you to put a semicolon there and save and run it again. +Now I actually want you to put a semicolon there and save and run it again. **
  
-Peruse the error output. Rust help is on the money! You will also see “rustc --explain E0308” at the end of the message. Run it and see. It is less helpful, but not a waste of your time. If you are having issues getting your head around it, there is another way. You can use the keyword “return” in front of that statement, but then you need to close it off with a semicolon.+Si vous avez vu qu'il n'y avait pas de point-virgule à la fin de la déclaration, c'est bon. Ceci trompe les plus novices. Mais il y a une règle de syntaxe. Vous voulez retourner quelque chose (une valeur est attendue), ce quelque chose venant de la DERNIÈRE ligne. (Dites-vous que le point-virgule supprime la sortie). Dans ce cas, vous ne devez pas utiliser le point-virgule à la fin de la déclaration (la « dernière ligne » est notre ligne unique). 
 + 
 +Si ce n'est pas clair ou si vous avez une meilleure façon de l'expliquer, merci de nous envoyer un mail à misc@fullcirclemagazine.org 
 + 
 +Maintenant, je veux en fait que vous mettiez un point-virgule à cet endroit, que vous le sauvegardiez et que vous le lanciez à nouveau. 
 + 
 +**Peruse the error output. Rust help is on the money! You will also see “rustc --explain E0308” at the end of the message. Run it and see. It is less helpful, but not a waste of your time. If you are having issues getting your head around it, there is another way. You can use the keyword “return” in front of that statement, but then you need to close it off with a semicolon.
  
 return num1 + num2;  return num1 + num2; 
  
 On from last issue: How did the number guessing game program work for you? In C, we include headers when using I/O functions. On from last issue: How did the number guessing game program work for you? In C, we include headers when using I/O functions.
 +
 +include <stdio.h>**
 +
 +Lisez attentivement l'erreur affichée. L'aide de Rust répond parfaitement ! Vous verrez aussi « rustc --explain E0308 » à la fin du message. Lancez cette commande et regardez. Elle n'est pas trop utile, mais ce n'est pas une perte de temps. Si vous avez des problèmes en essayant de comprendre, il y a une autre manière de faire. Vous pouvez utiliser le mot-clé « return » devant la déclaration, mais ensuite, vous devez la fermer avec un point-virgule.
 +
 +return num1 + num2;
 +
 +Continuons depuis le dernier numéro : Comment fonctionne le programme de jeu de devinette de nombre chez vous ? En C, nous incluons des en-têtes si nous utilisons des fonctions d'entrées/sorties (I/O).
  
 include <stdio.h> include <stdio.h>
  
-In Rust, we simply use the keyword ‘use’. The documentation describes it as prelude, but if it helps, you can think of it as a header-file. https://doc.rust-lang.org/std/prelude/index.html - I try not to get hung up on terminology, but if you don’t know the correct terminology, it becomes difficult to explain it to someone else. Call it Pinky if you like, as long as you know how to use it. If you followed Greg’s Python tutorial, you may have seen “from tkinter import *”. You need a way to pull in standard or non-standard libraries to use, so you don’t have to write everything yourself. The next thing I want to cover is the rand crate. The example uses the rand 0.5.5 crate, though we have moved on since then. At the time of writing, the rand crate is sitting at 0.7.3. See: https://crates.io/crates/rand +**In Rust, we simply use the keyword ‘use’. The documentation describes it as prelude, but if it helps, you can think of it as a header-file. https://doc.rust-lang.org/std/prelude/index.html - I try not to get hung up on terminology, but if you don’t know the correct terminology, it becomes difficult to explain it to someone else. Call it Pinky if you like, as long as you know how to use it. If you followed Greg’s Python tutorial, you may have seen “from tkinter import *”. You need a way to pull in standard or non-standard libraries to use, so you don’t have to write everything yourself. The next thing I want to cover is the rand crate. The example uses the rand 0.5.5 crate, though we have moved on since then. At the time of writing, the rand crate is sitting at 0.7.3. See: https://crates.io/crates/rand 
  
-The crates.io website is something you should bookmark. It is filled with handy libraries, but more on that later. I would go into depth explaining crates, but the website does it so much better. See: https://doc.rust-lang.org/cargo/guide/+The crates.io website is something you should bookmark. It is filled with handy libraries, but more on that later. I would go into depth explaining crates, but the website does it so much better. See: https://doc.rust-lang.org/cargo/guide/**
  
-You may have heard the joke, “there is no such thing as AI; it is if-statements all the way down”.+En Rust, nous utilisons simplement le mot-clé « use ». La documentation le décrit comme un prélude, mais, si ça peut vous aider, voyez-le comme une en-tête de fichier. https://doc.rust-lang.org/std/prelude/index.html - Je ne veux pas faire de fixation sur la terminologie, mais si vous ne connaissez pas les termes corrects, il vous devient difficile de les expliquer à quelqu'un d'autre. Appelez-le Pinky si vous voulez, tant que vous savez comment l'utiliser. Si vous avez suivi le tutoriel de Greg sur Python, vous aurez vu probablement « from tkinter import * ». Vous avez besoin d'une façon d'incorporer les bibliothèques standard et non-standard à utiliser, de sorte que vous n'ayez pas à tout écrire vous-même. La chose suivante dont je veux parler est le crate "rand". L'exemple utilise le crate rand 0.5.5, bien qu'il ait changé depuis. Au moment où j'écris, le crate rand est en 0.7.3. Voyez https://crates.io/crates/rand 
 + 
 +Le site Web crate.io est quelque chose sur lequel vous devriez mettre un signet. Il est rempli de bibliothèques pratiques et j'en reparlerai plus tard. J'aurais voulu expliquer les crates plus en détail, mais le site Web le fait beaucoup mieux. Voyez : https://doc.rust-lang.org/cargo/guide/ 
 + 
 +**You may have heard the joke, “there is no such thing as AI; it is if-statements all the way down”.
  
 Still nothing? I am here all week... Still nothing? I am here all week...
  
-Branching and making decisions is another must-know, when programming. We have for-loops, while-loops and if-statements. (There is also the loop-keyword you may not have seen). If you know what a case-statement or switch is, the equivalent in rust is the match-keyword. If you have no idea, don’t worry, we will look at it now. +Branching and making decisions is another must-know, when programming. We have for-loops, while-loops and if-statements. (There is also the loop-keyword you may not have seen). If you know what a case-statement or switch is, the equivalent in rust is the match-keyword. If you have no idea, don’t worry, we will look at it now. ** 
 + 
 +Vous connaissez peut-être la blague « L'IA  n'existe pas ; c'est plein de déclarations if ». 
 + 
 +Toujours rien ? Je suis là toute la semaine. 
 + 
 +Les branchements et les prises de décisions sont une autre partie indispensable à connaître, quand on programme. Nous avons les boucles for, les boucles while et les déclarations if. (Il y a aussi le mot-clé loop, que vous n'avez peut-être pas encore vu). Si vous savez ce qu'est une déclaration « case » ou une commutation, le mot-clé « match » est l'équivalent en rust. Si vous n'avez aucune idée là-dessus, ne vous inquiétez pas, nous allons le voir tout de suite.
  
-Match is not a dating app, it is a list of possibilities, or should that be probabilities. The only thing is, that it is finite. You do not want to make cases for every possible point on an elliptic curve. Match is specific. It is useful in, say, monkey puzzles, where you need to select the right answer, say from four possible answers, and display a message for your choice. You can even check more than one condition at a time too. Bottom left is an example.+**Match is not a dating app, it is a list of possibilities, or should that be probabilities. The only thing is, that it is finite. You do not want to make cases for every possible point on an elliptic curve. Match is specific. It is useful in, say, monkey puzzles, where you need to select the right answer, say from four possible answers, and display a message for your choice. You can even check more than one condition at a time too. Bottom left is an example.
  
 We can have multiple conditions by using the OR operator. Try it with 2 and 5. Bottom right is another example. We can have multiple conditions by using the OR operator. Try it with 2 and 5. Bottom right is another example.
  
-You can even match against a range (ellipses). It is deprecated, but it still works. See below left.+You can even match against a range (ellipses). It is deprecated, but it still works. See below left.**
  
-Most of this code is self-explanatory, except the underscore at the end. This tells rust we want to explicitly ignore all the other cases. If we don’t do this, rust will tell us about all the matches we missed. See below right.+Match n'est pas une appli de rencontre, c'est une liste de possibilités, ou, devrait-on dire de probabilités. La seule chose, c'est que c'est fini. Vous ne voulez pas faire des cas pour chaque point d'une courbe elliptique. Match est spécifique. Il est utile dans, disons, les Araucaria (Désespoir des singes) où vous devez sélectionner la bonne réponse, parmi, disons, quatre réponses possibles et afficher un message suivant votre choix. Vous pouvez même vérifier plus d'une condition d'un seul coup. Un exemple est montré en bas à gauche. 
 + 
 +Vous pouvez avoir des conditions multiples en utilisant l'opérateur OR. Essayez avec 2 et 5. Un autre exemple, en bas à droite. 
 + 
 +Vous pouvez même chercher des correspondances dans une suite (ellipses). C'est déprécié, mais ça marche encore. Regardez en bas à gauche. 
 + 
 +**Most of this code is self-explanatory, except the underscore at the end. This tells rust we want to explicitly ignore all the other cases. If we don’t do this, rust will tell us about all the matches we missed. See below right.
  
 If we have two dip switches to set the baud rate of serial communication, there are four possibilities, and Rust recognises that. It even tells us which one we missed! If we have two dip switches to set the baud rate of serial communication, there are four possibilities, and Rust recognises that. It even tells us which one we missed!
Ligne 61: Ligne 116:
 Next issue we can look at more conditionals. Next issue we can look at more conditionals.
  
-If you have any questions or comments, email us: misc@fullcirclemagazine.org+If you have any questions or comments, email us: misc@fullcirclemagazine.org** 
 + 
 +La plupart du code se comprend tout seul, sauf le trait de soulignement à la fin. Cela dit à rust que nous voulons explicitement ignorer tous les autres cas. Si nous ne le faisons pas, rust nous informera de toutes les correspondances que nous avons ratées. Regardez en bas à droite. 
 + 
 +Si nous avons deux mini-inters pour définir la vitesse de transfert d'une communication série, il y a quatre possiblités et Rust les reconnaît. Et il peut même nous dire quelle est celle que nous avons oubliée ! 
 + 
 +Dans le prochain article, nous pourrons regarder d'autres éléments conditionnels. 
 + 
 +Si vous avez une question ou un commentaire, envoyez-nous un mail à : misc@fullcirclemagazine.org
  
issue158/c_c.1593604922.txt.gz · Dernière modification : 2020/07/01 14:02 de auntiee