issue158: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édenteProchaine révisionLes deux révisions suivantes | ||
issue158:c_c [2020/07/01 14:02] – auntiee | issue158:c_c [2020/07/02 07:38] – d52fr | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | Okay, rustafarians, | + | **Okay, rustafarians, |
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, | ||
+ | |||
+ | 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. | ||
- | 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 < | ||
+ | |||
+ | Ç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 ça : | ||
+ | |||
+ | 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' | ||
+ | |||
+ | **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 de point-virgule à la fin de la déclaration, |
+ | |||
+ | Si ce n'est pas clair ou si vous avez une meilleure façon de l' | ||
+ | |||
+ | Maintenant, je veux vraiment que vous mettiez un point-virgule à cet endroit , que vous le sauvegardiez et que 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 < | ||
+ | |||
+ | Lisez attentivement l' | ||
+ | |||
+ | return num1 + num2; | ||
+ | |||
+ | Vu dans le numéro précédent : 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' | ||
include < | include < | ||
- | 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:// | + | **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:// |
- | 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:// | + | 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:// |
- | 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:// |
+ | |||
+ | Le site Web crate.io est quelque chose sur lequel vous devez mettre un signet. Il est rempli de bibliothèques pratiques, j'en reparlerai plus tard. J' | ||
+ | |||
+ | **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 « Il n'y a rien de tel que l' IA ; 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, | ||
- | Match is not a dating app, it is a list of possibilities, | + | **Match is not a dating app, it is a list of possibilities, |
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, | + | Match n'est pas une appli de rencontre, c'est une liste de possibilités, |
+ | |||
+ | Vous pouvez avoir des conditions multiples en utilisant l' | ||
+ | |||
+ | 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, | ||
If we have two dip switches to set the baud rate of serial communication, | If we have two dip switches to set the baud rate of serial communication, | ||
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. Ceci dit à rust que nous voulons explicitement ignorer tous les autres cas. Si nous ne faisons pas ça, rust nous informera de toutes les correspondances que nous avons raté. 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 reconnait. Et il peut même nous dire quelle est celle que nous avons oublié ! | ||
+ | |||
+ | Dans le prochain article, nous pouvons regarder d' | ||
+ | |||
+ | Si vous avez une question ou un commentaire, | ||
issue158/c_c.txt · Dernière modification : 2020/07/03 14:45 de andre_domenech