⚡Installation
I created a tutorial about how to install Rust on Youtube, Check it out if you prefer the video.
- Check out official page | https://www.rust-lang.org/tools/install
🔸 Install RUST
🔸Open the downloaded file
🔸Enter 1 to continue the installation
🔸Open VS Code and Install this extension — rust-analyzer
Then Check the version of the rust
rustc --version
Rust has been updated very often, so we should run this command as well.
rustup update
Create a new Project
cargo new hellow_world
Into the folder of new project
cd hellow_world
Build the project
cargo build
Finally we can run it.
cargo run
⚡What’s a variable?
Check out more in Rust Doc.
Assign data to a temporary memory location.
We can set a variable to any value & type,
- Immutable by default
- can be mutable
This is invalid.
fn main() {
let x = 5;
println!("The value of x is: {x}");
x = 6;
println!("The value of x is: {x}");
}
This is one of many nudges Rust gives you to write your code in a way that takes advantage of the safety and easy concurrency that Rust offers.
⚡Data Type
Rust is a statically typed language, which means that it must know the types of all variables at compile time.
- Integer
- Boolean
- double & float
- char
- string
Integer
Floating-Point Type
All floating-point types are signed.
fn main() {
let x = 2.0; // f64
let y: f32 = 3.0; // f32
}
Boolean
fn main() {
let t = true;
let f: bool = false; // with explicit type annotation
}
Char
fn main() {
let something = 'a';
let other_thing: char = 'A'; // with explicit type annotation
let heart_eyed_cat = '⭐';
}
String
fn main() {
let hello = String::from("Hello, world!");
println!("{}", hello)
}
Tuples
🔹Example 01 — Create some tuples
fn main() {
let tup = (500, 6.4, 1);
let (x, y, z) = tup;
println!("The value of y is: {y}");
}
🔹Example 02 — How to access the element of the tuple
fn main() {
let x: (i32, f64, u8) = (500, 6.4, 1);
let five_hundred = x.0;
let six_point_four = x.1;
let one = x.2;
}
Arrays
🔹Example 01 — Create some arrays
fn main() {
let a = [1, 2, 3, 4, 5];
let months = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
];
let b: [i32; 5] = [1, 2, 3, 4, 5];
let c = [3; 5];
println!("The value of a is: {:?}", a);
println!("The value of months is: {:?}", months);
println!("The value of b is: {:?}", b);
println!("The value of c is: {:?}", c);
}
🔹Example 02 —
fn main() {
let a = [1, 2, 3, 4, 5];
let first = a[0];
let second = a[1];
}
⚡println!
⚡Comment
Reference
- Variables & Mutability | Rust Doc