Outils pour utilisateurs

Outils du site


issue157:c_c

Ceci est une ancienne révision du document !


https://doc.rust-lang.org/book/ 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. In this issue, we will quickly go over data types, as each programming language has its own way of dealing with things like booleans, etcetera.

Let’s start with those while we are at it? Booleans in Rust are called ‘bool’. As in other programming languages, it is used in things like loops. However, if you have been following Greg’s excellent Python tutorial, you will know Python uses a capital T for True and a capital F for False. This is not the case with rust. You simply refer to them as ‘true’ or ‘false’. Characters are referenced by ‘char’. A character is always in single quotes and strings are in double quotes. This is a common newbie error as “a” and ‘a’ are NOT the same type. For numbers you need to know integers and floats. I have no doubt that if you have been following Greg, that you know what they are. I must point out, as we learned in the first part, there are ways to refer to them. Obviously, we have signed and unsigned integers, to represent negative numbers. This affects the size of the number you can use, as they take up a certain amount of space in bytes. You refer to signed with an ‘i’ and unsigned with a ‘u’. For example i32 or u64 as demonstrated in the last issue. For those of you not familiar with the terminology, signed (i) just means one of the bits is used for a sign. An easy way to remember which is which, is getting the Ace of Base song in your head. “I saw the sign…” or even “I store the sign…” (Yes, we use cheesy song references in our tutorials here at FCM, don’t judge.)

The way the numbering works is for example, i8 refers to; one byte for the sign, plus seven (7) bytes for the number. 1,2,4,8,16,32,64 = 127 (If you have used binary before, you would know that a row of each of the previous numbers add up to the next one minus one. The next number is 128, so I know that the sequence adds up to 127) If you had 8 bits, like with a unsigned number, (u8), you would move to the next bit, which is 256… (minus one), thus 255. Why? Because we can now use that bit we used for the sign in the signed integer. By default, if you use an integer like we did in the last issue, Rust will make it i32, which is seen as a ‘sane’ default. The same with floats or floating point numbers. It’s ‘sane’ default is f64.Just keep it in mind when you are on a 32-bit machine, like a raspberry pi. Now let us ramp it up one. Why did the programmer quit his job? …. He didn’t get arrays (a raise - geddit?) OK, I’ll see myself out…

What is an array? This is a ‘list’ of things, that are all the same type, grouped together. What I mean by this, is that an array does not mix say, strings and numbers. You can not have an array with a key, sword, a shield and the number 250 that represents your gold. If you want to have your salad mixed, you use a tuple. (We will get to it now.) The format for an array in rust is: let <variablename> = [item1, item2, item3]; To access an item in an array, the format is: let <variablename> = <nameofthearray>[position]; This sort of tells us how we can edit an array also. <nameofarray>[position] = newvalue; NOTE: I use the word “position” here, but feel free to substitute it with “index” as newbies find it easier to make the connection that way.

A word on this, make sure of your position or index before changing it, as it overwrites the previous value. To change an array, like the previous issue, we need the ‘mut’ keyword otherwise they are immutable. This ‘mut’ keyword may take some getting used to. Like integers, arrays start with a fixed size when you declare them. Let’s try it on for size? Navigate to your rust projects folder and open a terminal there. Type: cargo new –bin lesson2 Again, this will make what you need.

Remove the “hello world” and replace it with the code shown top right. Type: cargo build What happened? How now, brown cow? Rust suggests we use a vec. With vec’s we push and pop our elements. (More on this later.) By-the-way, at any time you want to know more about something, you can look it up by searching the phrase on doc.rustlang.org – try it: https://doc.rust-lang.org/std/vec/struct.Vec.html

During the quick once-over on arrays, I mentioned tuples. Again, if you have been following Greg’s Python tutorial, you may know where this is going. A tuple is a collection of things, like an array, but they do not have to be of the same type. (Like an array, indexing starts at 0) The following code is valid: Let mytuple = (donkey, true, 99); When referring to an item in a tuple, we use slightly different syntax to that of an array. The format is: let <variablename> = tuplename.position; In the case above, to get 99, you would say: let x = mytuple.2; You can even grab the lot at once, simply by: let (weapon,armour,gold) = mytuple;

Rust is smart enough to plug in donkey to weapon, true to armour and 99 to gold. Now you can keep your gold in your inventory with that extra shield and sword! (or donkey, *wink* it is your inventory after all) Go ahead and try the guessing game in the book: https://doc.rust-lang.org/book/ch02-00-guessing-game-tutorial.html Yes, I know it’s a boring example that sucks the marrow from your bones, but it teaches important concepts!

The reason I am using Geany as my IDE is it comes with Ubuntu and we don’t need to add things to it to make it work with Rust and it will gladly work with toml-files without complaint. It even sports it’s own terminal! All nicely in one place. If you have any questions or comments, e-mail us: misc@fullcirclemagazine.org

issue157/c_c.1590913239.txt.gz · Dernière modification : 2020/05/31 10:20 de d52fr