Rust Data Types

The official reference is here. It is a wonderful and well-written resource. As a learner, I want to write things down in my own languages or repeat what has written there.

Rust is a statically typed language. Just like Java, C#, … it must know the types of all variables at the compile time. Usually, the compiler does that by two means. One is that types are supplied explicitly. The other is by inferring from the value, the complier makes the best guess from a value.

I usually prefer the explicit type approach, especially numeric values.

// Explicit declaration
let age: i32 = 38;

// Implicit declaration. The compiler will figure out the type
let age = 38;

A few things from that simple statement

  • let: Rust keyword to define a variable
  • age: variable name
  • :: seperator between variable name and type
  • i32: variable type, in this case, it is a 32bit integer. Variable and type are seperated by a colon :
  • =: assignment, assign a value to the variable
  • 38: variable value

Shadow and Mutate

Shadow is the ability to define a new variable with the same name. It means that the "new" variable can be of a different data type.
Rust is immutable by default. To mutate a variable, it might be declared with mut keyword. It requires extra attention from the developers. You should know what you are trying to do.

fn shadow() {
    let x = 5;
    let x = x + 1; // which is 6

    {
        // Create an inner scope
        // shadow the x variable
        let x = x * 2;
        println!("The value of x in the inner scope is: {x}");
    }

    println!("The value of x in the outer scope is: {x}");
}

fn mutate_scope() {
    let mut x = 5;
    x = x + 1; // which is 6

    {
        // Create an inner scope
        // shadow the x variable
        x = x * 2;
        println!("The value of x in the inner scope is: {x}");
    }

    println!("The value of x in the outer scope is: {x}");
}

Tupe and Array

Tupe allows developers to construct a new structure (or data type) that holds different data types.
Array is a fixed size structure of the same data type.

fn data_types() {
    // Besides the normal types seen in many other languages, tupe and array are interesting to explore here
    let tup: (i32, &str) = (38, "Thai Anh Duc");
    println!("Hi, my name is {}, {} years old", tup.1, tup.0);

    // Deconstruct tup into individual variables
    let (mut age, mut name) = tup;
    println!("Hi, my name is {name}, {age} years old");

    age = 10;
    println!("Hi, my name is {name}, {age} years old");

    name = "TAD";
    println!("Hi, my name is {name}, {age} years old");

    // Array is fixed size
    let _months = ["January", "February", "March", "April"];
    // Auto generated values from a feed one
    let auto_values = [10;3];
    println!("{}", auto_values[2]);
}

With those basic data types, one can write an application.

Write a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.