Ceci est une ancienne révision du document !
En cours, ailleurs ! Voici l'anglais… tu pourras toujours l'effacer si ça rend les choses plus difficiles…
https://doc.rust-lang.org/book/
Okay then, we head back to Rust development – as Daredevil14 and Ellin complained about where Lucas’ blog went. Rust seems to be getting more popular as time goes by.
Truth be told, I do not like a language where a simple ‘hello world’ is 2Mb(!!), just because “space doesn’t matter”. Okay, my rant over. Regardless, we are going to look at it as the feedback suggests that this is what our readers want.
For everyone who wants to learn more, I will go through installing Rust first, so you can follow along if you like. This article explains how to install Rust in Ubuntu 18.04 (but 19.x should be the same), using the 'rustup' tool. Rustup is a terminal tool that is used to manage the installation of rust versions and optional components. Rust currently has a six-week release cycle, so ‘rustup’ is a good idea. Head on over to: https://rustup.rs/ - and run that curl command to get it installed. Choose option one (1) if prompted. Restart once done. (Easiest, as this will add what you need to your environment variables).
If the download is interrupted, you can simply go back and run the command again, and you will have the option to continue with the installation.
Once it is done, run:
rustc –version ( dash dash, no spaces)
cargo –version ( dash dash, no spaces)
If you get a reply, all went well.
** At the time of writing, rust is version 1.41.0. If yours differs, that is OK. Just make sure it is a higher number, not a lower one.
Look up: “men at work - cargo” in your browser (an ancient Australian band). You will see an image of a plane and a crate. I will be using binary crates, as it is easy for n00bs like you and me. Think of cargo in this way. Delivering crates, containing what you need. Maybe even listen to “Dr Heckyll and Mr Jive”, now that you have looked it up?
Type:
cargo new –bin rustfun
(The other option I know of is: “–lib” for library files).
A new binary crate named rustfun will be created (a folder and skeleton files).
When you open main.rs in the src folder, you should see a simple ‘hello world’ already in there for you. Before you entered the src folder, you should have seen a cargo.toml -file. You can open this in a text editor or in Geany if you plan on using it as your IDE, or even just cat it out. We can look at all these folders and files a bit later on; for now, I just want to highlight a few things.
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 its own terminal! All nicely in one place.
Here is what the installation looks like on my machine:
Great, now that you have it installed, let’s look at the basics and how rust treats each one.
Variables
Like other programming languages, a variable is just a container for a value, regardless of type. In rust, all variables have types (more later). When we program, we simply refer to the variable by name.
Variable assignments in Rust are prefixed by the word “let”. For example: let my_box = 1; Also, when you assign a value to a variable (when you declare it), you cannot change its value (it is immutable). The following WILL give you an error, because of the above reason (shown below).
We will cover more later, but for now, just know about this in Rust. The command ‘cargo run’, will simply tell you that it is immutable. Rust is supposed to be error resistant, so it assumes that if you don’t explicitly tell it that a variable can change, it can’t. Okay… How do we do that? With the ‘mut’ keyword, example: let mut my_num = 1;
Just by simply adding that “mut”, the error is gone and our program compiles (shown bottom right).
Remember I said all variables in Rust have a type? Well, Rust figures out the type in the background for you. This does not mean Rust type-casts your variable sweaty_shopowner as ‘sleazy’, rather as a string. Should Rust get it wrong, or you are a masochist, simply add a colon after the variable name and the type thereafter.
Example: let my_shoesize: i32 = 13;
Next issue: we can move on to another part of Rust and discuss how Rust treats that, say loops / conditionals? (Find me on Telegram if you want something else).
Now a quick word on the files and folders.
Once you build or run your file, you will see a new file named “cargo.lock”. This file is automatically generated from your cargo.toml file. Be sure your details are correct in the cargo.toml file, before building. There will also be a “target” folder created. Inside will be a debug folder with lots of sub-folders. Feel free to peruse these at your leisure. There should also be an executable file with the same name as your project. Run it now to see if your rust program works. Mine does (shown top right).
The code used in this demonstration is shown right.
Let’s step through it.
Every Rust program needs a main function, that is: fn main() {}
Inside our curly braces we have our variable assignment, that we talked about.
We incremented the variable with 1, the same as num = num +1
Then println!(“”); will print anything we put between the “” (quotation marks).
What you may not know is that {} inside of the print function; it’s a place holder to plug in a value. The value we plug in is outside of the “” and after a comma. In our case, the variable, num (more on format specifiers later).
If you have any questions or comments, e-mail us: misc@fullcirclemagazine.org