Guidelines to get hands on into Rust.
- Concurrency, Safety, Speed. How?
- Safety AND Speed
- No Garbage Collector
- Excellent memory manager sifting the program using Stack and Heap data structuring.
- Ownership and Borrow Checker
- And other things like being Easy to compile it to WebAssembly, immutability by default, detailed type annotations...
- Ownership is Rust’s most unique feature, and it enables Rust to make memory safety guarantees without needing a garbage collector.
- The rule - a variable can :
- either have a one mutable reference to it
- or multiple immutable references to its data
- never both.
- Example shown in this article
- Borrow checker listens to this rule and gives you compile-time errors indicating you must follow the rule strictly.
Some of the red dots on Rust.
- All these new paradigms can be a bit complicated and slow you down into creating new stable products
- Learning curve is steep
- Community has some room to grow
- Get ready for a somewhat ugly syntax.
You can browse Rust Libraries here:
MacOS needs xcode and corresponding development tools:
xcode-select --install
Windows:
- ¯\(ツ)/¯
Linux needs gcc installed. Depending on your system package manager you'll probably have the package gcc available to install.
APT Example :
sudo apt install gcc
This fetches the installer and pipes the downloaded file into executing it. It will install Rust and Cargo. Cargo is Rust's package manager.
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
By the end the installer will show you this message:
Rust is installed now. Great!
To get started you may need to restart your current shell.
This would reload your PATH environment variable to include
Cargo's bin directory ($HOME/.cargo/bin).
To configure your current shell, run:
source "$HOME/.cargo/env"
It's important that your path recognizes Rust folder for executables "bin". So if you're command lines are not being recognized, please check your PATH env variable.
Type the following command to update Rust. (Also to check if your terminal recognizes your updated PATH env var)
rustup update
For example, let's do something we'd usually do on javascript into a file called im-coding-js-in-rust-lol.rs .
function slapSomeone(yourName, targetName) {
return yourName + " slaps " + " around a bit with a large trout";
}
Try to compile this. you already know it's not going to run. But see if you can correct the program without resorting any stack overflow shenanigans. Rust compiler should illustrate problems like it's the van gogh of ascii characters.
rustc im-coding-js-in-rust-lol.rs
The value initialized into the variable cannot be changed unless specifically specified.
fn main() {
let x = 5;
println!("The value of x is: {x}");
x = 6;
println!("The value of x is: {x}");
}
This will give you a compile time error. See if you can make this program work out.
Data types are special annotations that you can specify after the variable. They can be inferred by its initial value or you can specifically tell them what they are.
let a = 2; //i32
let b: i32 = 2;
The list of types you can annotate are here
Like in many modern iteration of languages, for you to use a function you need you need to declare that you're using it. You need to include it on the scope you're working.
Example
use std::cmp::least;
Rust website offers you 3 approaches:
- Book
- Rustlings Course
- By Examples
Get book. Must read non-fictional book.
This gives your the basics. Instalation, Cargo package manager, your first program with user interaction. And then much much more.
Rustlings is an open source project you can clone to do Rust exercises:
A list of examples in Rust, demonstrating most Rust's programming courses .
After these exercises you can start doing basic HackerRank exercises to up your Rust face on:
- [HackerRank Algorithm Exercises (Must Choose Rust Language)] https://www.hackerrank.com/domains/algorithms