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.

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

--- primaryColor: steelblue shuffleQuestions: false shuffleAnswers: true --- # Which of the following is a scalar data type? - [ ] Integer - [ ] Float - [ ] Tuple - [ ] Array # In Rust it is a must to define the type of the variable. - [ ] true - [ ] false

Last updated 25 Jan 2024, 05:11 +0530 . history