DEV Community

Cover image for Back to Basics With Rust: Part 2
Silva
Silva

Posted on

Back to Basics With Rust: Part 2

Continuing from part one. I think you now put more thought in declaration. When writing code, It's also good to understand the terminologies and classifications because, that does not change much when moving from one programming language to another. In this article I continue going into detail about declaring of variables (using comment lines starting from 1 to 7) and also classifying them. By classifying I mean talking about primitive types. Primitive data types are divided into two, scalar and compound types.

  • Looking at comment line no. 1 : The declared variables below are example of scalar types.
    • Scalar type represents a single value. e.g char, integers, floating points and boolean.
  • Comment Line no. 2 to 7 are examples of compound types.
    • Compound types represents multiple values. e.g array and tuples.
fn main() {
    // Scalar -----------------------------------------  1 
    let s_char : char = 's';
    let is_bool : bool = true;

    // Compound ---------------------------------------- 2
    let mut array: [i32; 3] = [0; 3]; // --------------- 3
    array[0] = 1; // ----------------------------------- 4
    let array: [i32; 3] = [0, 1, 2]; // ---------------- 5

    let (tu, ple): (i32, char) = (32, 'a'); //---------- 6
    let tuple: (i32, char) = (32, 'a'); //-------------- 7
}
Enter fullscreen mode Exit fullscreen mode

Declaring compound types:

Comment line no. 3 and 5 are Arrays

  • Arrays are fixed size collection of values with similar types.
  • Once declared can not grow.
  • Comment no. 3 above:
    • [i3; 3] indicate that the array will be of type i32, with the size of 3 (size is always a positive value).
    • [0; 3] is similar to [x; N] is called a repeat expression, which will produces an array of [0, 0, 0]
    • mut mean the array is mutable. which allows the value inside an array to be changed or replaced.
  • comment no. 4 :
    • 0 inside array[0] indicates the index, index starts from 0, end at [length - 1].
    • e.g array [0, 0, 0] index will start from 0 and end at 2
    • specifying a bigger index like array[4] can result to index out of bounds: the len is 3 but the index is 4
  • Looking at comment no. 5 and 3, they are almost identical, both are declared as [i32; 3]
  • the only different is that comment no. 5 has a collection of integers i.e., [0, 1, 2], not a repeat expression.
  • also notice 3 and 5 have the same name(both called array). that concept is called Shadowing.
Shadowing
  • is to declare a new variable with the same name as a previous variable as shown in 3 and 5
  • the compiler will ignore the first variable meaning if we try array[0] = 1; below comment no. 5, get the below error :

    error[E0594]: cannot assign to `array[_]`, as `array` is not declared as mutable
    


    `

  • Why use shadowing ?

    • for beautiful code, instead of using array_one and array_two (some might disagree).
    • for casting, if you want to change types.
    • When you have multiples scopes or closures, shadowing can be useful (will get into details in the future).

Comment line no 6 and 7: are Tuples

  • A tuple is defined as a collection of values that consists of different or similar types (tuples are heterogeneous).
  • tuples have a length, looking at comment no. 6 and 7 the length of the tuple is 2
  • tuples also have a sequence.
    • comment no. 6 tu is the first sequence and ple is the second sequence
    • comment no. 7 when calling the first sequence, we use tuple.0

Visiting the rust doc will help you get more information on primitives types such as never which i didn't tackle.

Top comments (0)