Installation
Let us start setting up our environment by following what is in the official documentation.
You can check it out in here.
We should just copy the next line and paste it in our terminal:
$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
You can select the default installation option by typing 1
.
After this you should go to your ~/.zshrc
or ~/.bash_profile
and add at the end the next line:
export PATH="$HOME/.cargo/bin:$PATH"
When you have completed this previous step, go back to your terminal and then type:
$ source ~/.zshrc
With this you will have the RUST tool available in your terminal.
You can verify the installation and that you have added the tools to your path successfully when you input:
$ rustc --version
##Output: rustc 1.41.1 (f3e1a954d 2020-02-24)
Alternative to installation.
If you just want to play with RUST without installing it in your machine, you can do it in its official playground.
Your first "Hello, World!" with RUST
Now that you have the RUST tools available in your environment, let us create a new file called hello.rs
:
touch hello.rs
And inside this file we will copy and paste the following:
fn main() {
println!("Hello, world!");
}
After we added the code inside the file, we can go back to the terminal and type:
rustc hello.rs
The result of the command will be an executable file that should output: Hello, world!
.
To execute the file in MacOS you can type:
$ ./hello
## Output: Hello, world!
Using a Package Manager named Cargo
Nowadays almost every tool has its own package manager, which makes our lives way more easier than in the past. The package manager for RUST is called Cargo and you have already installed if you have followed the steps in the first section of this article.
To start using it, we will need some things:
- Have a folder named
src
- Have a file named
main.rs
for applications orlib.rs
for libraries. - A TOML file named
Cargo.toml
- Run
cargo build
orcargo run
if you are in a Mac you can type the following commands:
$ mkdir src
$ mv hello.rs ./src/main.rs
$ touch Cargo.toml
Inside your Cargo.toml
file, you can put:
[package]
name = "hello_world"
version = "1.0.0"
authors = ["YourName <xxxxx@gmail.com>"]
Once you have written this, you can build your RUST app with:
$ cargo build
and to run it
$ cargo run
As output you must me seeing now:
Finished dev [unoptimized + debuginfo] target(s) in 0.00s
Running `target/debug/hello_world`
Hello, world!
Shortcut to use Cargo
If you do not want to go through the hassle of doing all of the previous step, you can use a command that cargo offers:
$ cargo new my_rust_app --bin
And Voila!
You have now the same files we've just created.
A simple function
As a natural next step, you would like to create a function.
Let's have a simple function that will sum two signed integers that each takes up to 8 bits of space.
fn sum(x:i8, y:i8) -> i8{
x + y
}
We need to update our main.rs
to print out the result from calling the new function:
fn main() {
let a:i8 = 1;
let b:i8 = 2;
let mut total:i8 = sum(a,b);
println!("a = {}, b = {}, a+b = {}", a, b, total);
total = -100;
println!("New total = {}", total);
}
Now run cargo run
in your terminal, and you should see the beauty of rust.
Try to change some things in the main method, to see how RUST will point out the possible mistakes you have.
The RUST Book - Documentation
If you want to learn more about RUST you can check its documentation in HERE.
And about Data Types in this section of the book.
End
Hey people, I hope you like this small tutorials, and let me know if you want to learn something else or any other feedback you may have.
Cheers!
Top comments (0)