Link Search Menu Expand Document

What Are Data Types?

  • Data Type

Rust is a statically typed language, meaning, it must know the type of all variables at compile time.

How to Define a Type in Rust?

We can define a variable in rust in two different ways:

  • Implicit Definition

Unlike other languages like C++ and Java, Rust can infer the type from the type of value assigned to a variable. Syntax #

The general syntax is:


let variable name = value ;

Explicit Definition

Explicitly tells the compiler about the type of variable.

Syntax

The general syntax is:

let variable name : data type  = value ;

Primitive Types

Rust has a couple of types that are considered primitive. That means they are built-in to the language. There are different data types used for different purposes.

The following illustration shows the different primitive data types in Rust:

Scalar Type

They store a single value.

  • Below is the list of scalar types:
    • Integers
    • Floats
    • Boolean
    • Character

Compound Type

They group multiple values in one variable. Below is the list of compound types:

  • Array
  • Tuple

Quiz

  1. Which of the following is a scalar data type?
    a) Integer
    b) Float
    c) Tuple
    d) Array

  2. In Rust it is a must to define the type of the variable.
    a) true
    b) false