Outils pour utilisateurs

Outils du site


issue159: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’ve got you covered here at FCM.

These articles are not to replace the book mentioned above, think of them more as helpers along the way.

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. If you have been following Greg’s excellent tutorials, you would be familiar with local and global variables. (The scope of the variables). The thing in Rust is, every variable has an owner, which is a variable too. Sounds confusing? Come read chapter 4.1 with us!

This will be chapter four in the book above. This is the direct link: https://doc.rust-lang.org/book/ch04-01-what-is-ownership.html

What does it mean to own data? Why would you need to own data? Is there a tax to be paid for owning data? Well, from what I can tell, there is no performance hit, but you take a hit in file size. When you compile a tiny ‘hello world’ console application, you end up with 2MB instead of 2KB. Let me try to break it down. Each piece of data has an owner, which is obviously a variable. That owner is responsible for cleaning up after that data. The owner decides if the data can be changed (is it mutable?) This “cleanup” happens when the owner goes out of scope. What do we mean by this? When you create a function, you have starting and ending curly braces. Any variable within those curly braces has scope only there, so when your program steps out of the last curly brace, the magic happens. Just for those a bit more curious, that “magic” is called “drop”.

How about we use a real world allegory: your friend Tammy has a games night. You bring your favourite card game, Exploding Kittens. Everyone else brings their favourite game too. When you are done playing Exploding Kittens, you, as the owner, pick up all the cards on the table and pack them back into the container and take it home with you when you leave. If you leave the cards lying on the table, it would make it difficult to play the next card game. You also won’t be invited back if you expect someone else to pack up your cards after every game. You may have heard that some programming languages have “garbage collection”. This is great, but there is a performance hit as this program needs memory and CPU cycles to clean up automatically after your mess.

Remember when we declare a variable in Rust, we have the option to use the “mut” keyword. Now obviously, when you change a variable, its size in memory changes too, imagine changing a string “FCM” to this whole article, three characters vs three thousand. So the memory allocation can change. And that extra may need to be cleaned up. In the example in the Rust book, you may have noticed: “ let mut s = String::from(“hello”); ” I am going to oversimplify this, but hear me out. This now creates data, say five bytes, each containing one character from that string “hello”. So you have the first byte being an “h”, the next an “e”, and so on. The “ :: “ notation is new to me too, so don’t feel left out. For now, simply do it that way and memorise how it is used. Don’t worry, if you don’t understand the references in the examples in the book, they refer to other programming language syntax, to help programmers who are familiar with those other programming languages transition to Rust more smoothly.

Because the owner of the data is a variable, that too, can change. Consider declaring a variable a, then assigning a to b (let a = “FCM” then let b = a). You have now transferred ownership from a to b. But, Aber, Pero, Ale. What if you want to keep using your original declaration? Well, then Rust has cloning. This allows you to keep ownership of your data, but also give ownership to another piece of your code. If you see an error: “value used here after move”, you probably need to clone. By the way, that error is really descriptive, it tells you that you tried to use a variable’s value after it was moved to another owner (like in the example above). Now cloning takes up that same amount of memory again, so be careful when cloning. Don’t just clone willy-nilly. To clone a variable, you add the “.clone” to your variable when you use it. So a.clone() will let you clone the “FCM” value in our example above. If you need to, re-read this section in the book, I certainly did!

Now that you have an idea of ownership, next time we can talk about things like borrowing. As with anything, practice makes perfect. Try out the examples in the book, make mistakes, learn what the error messages mean. I know how frustrated I was when all I kept getting was “syntax error” after “syntax error”. At least Rust is a lot friendlier. Don’t be shy to try something, the worst that can happen is that you can die. Wait, that is not correct? Syntax error… :)

I urge you to go through the whole section above, right down to part 4.4. We can delve into this later, but for now I want you to get to grips with why this is important and why this sets Rust apart from other programming languages. Now, I don’t know Rust inside out, nor do I know all the nuances of programming, but as a newbie myself, I can give some newbie insights and things I have figured out for myself. Remember the book above is written for programmers, by programmers. As a newbie to Rust or programming, you may not always get what they are trying to bring across. This is where your friendly FCM team comes in, as we try to help you bridge that gap. If I made any mistakes, or if you have some insights to add that may aid new programmers and rustafarians alike, please drop us an email at: misc@fullcirclemagazine.org

We welcome the feedback, as it helps us too.

issue159/c_c.1596382861.txt.gz · Dernière modification : 2020/08/02 17:41 de auntiee