In the last post we installed rustc
or Rust compiler and wrote a little hello world program, compiled it and executed it and now we will be learning about the basics of Rust programming language. These include Variables, Comments, Types etc.
Comments
To write comments in Rust programming language we use double backslashes and triple if you want to write documentation comments. Documentation comments also support markdown syntax.
// This is a comment in Rust
// This is another comment
/// This is a Documentation Comment
/// And it supports Markdown syntax
/// Like
/// # Documentation
/// [This is a Link](https://zainsci.github.io)
Variables
To assign variables in Rust we use let
keyword followed by the variable name and an equal sign =
and the value of the variable. Rust is a statically and strongly typed language that means that you either tell the compiler what type a variable is or it will guess it self.
fn main() {
let a: i32 = 10; // a is a 32-bit integer type
let b = 10; // b is also a 32-bit integer type
}
Here i32
is the interger type and it is a 32 bit integer. If you assign a variable again somewhere with the same name it will destroy the previous one and will store the new value with the new type in it.
fn main() {
let a: i32 = 10;
let a: &str = "Hello World!";
println!(a); // Hello World!
}
Another thing about variables in Rust is that all the variables assigend are immutable in Rust by default meaning that if you try to change the value of a variable it will throw an error tellin you that the variable is immutable.
To go around this we can use a keyword mut
after the let
keyword to tell the compiler that this var is mutable and will be changed later in the program.
fn main() {
let a = "immutable"; // This var is immutable
a = "something"; // Will throw an error
let mut b = "mutable"; // This var is mutable
b = "something"; // Will not throw an error
}
But the compiler will also complain to you if you add mut
before a var but didn't changed its value anywhere in the code.
Rust compiler will also complain to you if there is a unused var in the program and to handle that error the complier will also tell you to add an underscore _
before the var.
fn main() {
let a = 1; // Error: unused variable
let _b = 1; // No error
}
That's not all about variable theres more to them but for now this is all we need to have to take care of and now up to types.
Types In Rust
There are many types in Rust language and the ones that I may have read about for the first time myself but we will not be looking at all of them for now. We will onyl know some of them that will be enough for us to start working with Rust.
- Boolean types
- true - For truthy value
- false - For falsy value
- Integer types
- signed interger types
i8
- 8-bit integer from 0 to (2^7) - 1i16
- 16-bit integer from -(2^15) to (2^15) - 1i32
- 32-bit integer from -(2^31) to (2^31) - 1i64
- 64-bit integer from -(2^63) to (2^63) - 1i128
- 128-bit integer from -(2^127) to (2^127) - 1
- unsigned interger types
u8
- 8-bit integer from 0 to (2^8) - 1u16
- 16-bit integer from 0 to (2^16) - 1u32
- 32-bit integer from 0 to (2^32) - 1u64
- 64-bit integer from 0 to (2^64) - 1u128
- 128-bit integer from 0 to (2^128) - 1
- signed interger types
- Floating-Point numbers
f32
- binary32 floating-point numbersf64
- binary64 floating-point numbers
- Textual types
char
- Char type for unicode values&str
- String type for string values
Next will be funcions and some more basics.