Hello ππΎΒ , welcome !
I am learning Rust and I am finding much of the language very interesting and I am falling in love already , haha.
Either ways I wanted to blog some the topics that I am learning and Find interesting and I am staring with the basic!
Variables
Few points about variables in Rust.
-
Every variable is immutable by default
-
to make it mutable you can just add
mut
-
Variables are ==! constants
-
Constants are always immutable , you can make variables mutable or not
-
Constants should be annotated!
1// varaibles with default immutablity2let a = 4;34// mutable vairables5let mut a = 467// convention to make your constant Capital and snake case π8const EXAMPLE_CONSTANT:u32 = 444;
Constants βοΈ
Constants can be declared on any scope
- Global scope
- any other scope
In Rust
Scopes are determined by {}
unlike
JSwhich depends on the declaration keyword you use π€¦πΎββοΈ.
If you use
varits function scoped while
letand
constact on a
{}scope!
Constants could only be set to a constant expression that include a variable with a known value before runtime
1const FIRST_CONSTAT:u32 = 33*33; β2// So you can't use mutable variable to compute value of a constant!3const SECOND_CONSTAT:u33 = a*4; β45let mut ex = 3;67// this is going to throw an error compile time!8const THIRD_CONSTANT = ex*4 β
I.e rust uses snake-case with capital for constants as a convention!
Shadowing π₯·πΎ
Shadowing happens when you hide a variable in a higher scope or same scope by using the same variable name.
Shadowing is common in many programming languages and somehow the same rule applies to
rustbut rust shadowing was weird for one reason.
-
You can shadow a variable in the same scope π€―
If you tell this to
JS
It would probably crash!
1// declare a variable with name a2let a = 33;34// Error ( variables are immutable )5a = 32;6// this is valid in rust ( same scope level shadowing)7let a =32;89fn deeper() {10 // now a is different11 let a = 44;12}
The above code will throw an error for immutability ! but re-declaring with the same is fine with
rustπ¦
Data types in Rust
Rust have two types of data
scalerand
compound
Scalar Data types are four
- Character
- Integer
- Float
- Boolean
Here notice string is not a scalar data type!
Characters
Characters are four byte size data types that can hold unicodes , and characters in different languages.
Integers
Integers can be signed or unsigned and can hold different sizes of data. the well known sizes are the following
- 8 ( u8 , i8)
- 16 (u16 , i16 )
- 32 (u32, i32 )
- 64 (u64 , i64 )
- 128 ( u128 , i128 )
- arch (usize , isize) - this depends on the arch of your computer - if your computer is 64 bit it will be 64 bit , if not 32 bits or what ever the arch of your computer is!
so for signed Integers
1 bit is used for sign so the actual value the number variable can hold is 2^(n-1) for any n while
for unsigned it would just be
(2^n)-1
Number literals
Number literals are exciting way of writing different base of numbers in that languages - the first four are basically same as JS except for using
_(which is basically the same as
,in Real life π)
Number literals | Example |
---|---|
Decimal | 98_222 same as 98,222 |
Hex | 0xff |
Octal | 0o77 |
Binary | 0b1111_0000 |
Byte (u8Β only) | b'A' |
Integral overflow
I suggest you donβt overflow your
intbut if you do be aware of how rust handles it in Prod. ππΎ
Float Numbers
represented as single or double permission , implemented the same as JS , IEE-754 standard.
Sounds familiar ? JS has the same standard too!
1// single percision2let a: f32 = 44.4;3// double percision ( default )4let b: f64 = 44.33;
Integer Operations truncate toward zero ( for +,-,*,/)
compound types
There are two primitive compound types
- Tuples ( array of different types )
- Array ( type of similar types ) - Array have fixed size and data is allocated on the stack rather than the heap.
1// tuple with two i32 bits2let tup = (33,33);34// type for tuples5let person: (str,u8) = ('abebe', 22)67let arr1 = [33,44]89// which is the same as below (with data annotation)10let arr1:[i32;2] = [33,44]
One main difference between those two compound types and other collections you encounter in
rustlike
vecand
Stringis that these compound types live in the stack because