Outils pour utilisateurs

Outils du site


issue158:c_c

Ceci est une ancienne révision du document !


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:

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!();

In C, one specifies the type of return value before the function.

int main (void)

In Rust they use an arrow. To make the arrow, simply type a dash followed by the greater than symbol. “→”

fn <functionname>(parameters) → returntype {}

This may seem a bit odd… If not, it seems that way to me.

For simplicity’s sake, let’s assume we want to make a function that adds two numbers and returns the answer. It may look something like this:

fn adder (num1: i32, num2: i32) → i32 {

num1 + num2

}

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).

If this is unclear or you have a better way to explain it, please email us on: misc@fullcirclemagazine.org

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.

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.

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

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”.

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.

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.

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.

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!

Next issue we can look at more conditionals.

If you have any questions or comments, email us: misc@fullcirclemagazine.org

issue158/c_c.1593604922.txt.gz · Dernière modification : 2020/07/01 14:02 de auntiee