Debug School

rakesh kumar
rakesh kumar

Posted on

A Complete Guide to Data Types in Rust Backend Languages

Scalar Types

Integers
Floating-Point Types
Boolean (bool)
Character (char)

Compound Types

Tuples
Arrays

Notes on Rust Types

Interview Questions & Answers on Rust Data Types

MCQs on Rust Data Types (With Answers)

Rust is a statically typed language, which means every value has a specific type known at compile time. The compiler often infers types, but sometimes you need to annotate them explicitly so Rust knows exactly what you mean.

Rust’s data types are broadly grouped into Scalar and Compound types:

Scalar Types

Scalar types represent a single value. Rust has four primary scalar types:

Integers

Whole numbers without a fractional part.

Examples include:

i8, i16, i32, i64, i128 (signed)

u8, u16, u32, u64, u128 (unsigned)
Enter fullscreen mode Exit fullscreen mode

isize, usize (pointer-sized)

let a: i32 = 42;
let b: u8 = 255;
let c: isize = 10; // size depends on platform
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ Signed integers store negative and positive values; unsigned hold only zero and positive.

▢️ 2. Floating-Point Types

Numbers with decimals.

let x = 3.14;        // f64 (default)
let y: f32 = 2.5;    // f32
Enter fullscreen mode Exit fullscreen mode
f32 β€” 32-bit float

f64 β€” 64-bit float (default)
Enter fullscreen mode Exit fullscreen mode

▢️ 3. Boolean (bool)

True or false values.

let is_active: bool = true;
let is_done = false;  // type inferred
Enter fullscreen mode Exit fullscreen mode

▢️ 4. Character (char)

A Unicode scalar value, not just ASCII.
Enter fullscreen mode Exit fullscreen mode
let letter: char = 'R';
let emoji: char = 'πŸ¦€';
Enter fullscreen mode Exit fullscreen mode

Each char is 4 bytes, capable of representing many languages and emojis.

Compound Types

Compound types group multiple values into one type.

Tuples

A fixed-length collection of values of possibly different types.
Enter fullscreen mode Exit fullscreen mode
let tup: (i32, f64, u8) = (500, 6.4, 1);
let (x, y, z) = tup; // destructure
Enter fullscreen mode Exit fullscreen mode

You can access elements by position:

println!("{}", tup.1); // prints 6.4

Arrays

A fixed-length collection where all elements must have the same type.
Enter fullscreen mode Exit fullscreen mode
let nums: [i32; 5] = [1, 2, 3, 4, 5];
let repeated = [3; 5]; // [3, 3, 3, 3, 3]
Enter fullscreen mode Exit fullscreen mode

The length is part of the type.

πŸ“Œ

Notes on Rust Types

🧠 Type Inference

Rust often infers the type from context, so you don’t always need an explicit annotation.

β›” Type Safety

Trying to assign a value of one type to a variable of another type without casting causes a compile-time error.

πŸ§ͺ Quick Example Program

fn main() {
    // Scalar example
    let age: u32 = 30;
    let price = 99.99; // f64 inferred
    let is_ready: bool = true;
    let symbol: char = 'R';

    // Compound example
    let person: (&str, u32) = ("Alice", 28);
    let numbers: [i32; 3] = [10, 20, 30];

    println!("{}, {}, {}, {}", age, price, is_ready, symbol);
    println!("Name: {}, Age: {}", person.0, person.1);
    println!("Array first element: {}", numbers[0]);
}
Enter fullscreen mode Exit fullscreen mode

πŸ’‘

Summary

Category    Types
Scalar  i8, u32, f32, f64, bool, char
Compound    Tuples (T1, T2, ...), Arrays [T; N]
Enter fullscreen mode Exit fullscreen mode

These built-in types give Rust predictable, efficient, and safe handling of dataβ€”key for systems programming.

Interview Questions & Answers on Rust Data Types

Basic Level

  1. What are data types in Rust?

Answer:
Data types define the kind of data a variable can hold. Rust is a statically typed language, meaning all variable types must be known at compile time.

  1. How are data types classified in Rust?

Answer:
Rust data types are mainly classified into:

Scalar types

Compound types
Enter fullscreen mode Exit fullscreen mode
  1. What are scalar data types in Rust?

Answer:
Scalar types represent a single value. Rust has four scalar types:

Integers

Floating-point numbers

Boolean

Character
Enter fullscreen mode Exit fullscreen mode
  1. Explain integer data types in Rust.

Answer:
Integer types store whole numbers and are divided into:

Signed (i8, i16, i32, i64, i128)

Unsigned (u8, u16, u32, u64, u128)

Pointer-sized (isize, usize)

let a: i32 = -10;
let b: u32 = 20;
Enter fullscreen mode Exit fullscreen mode
  1. What is the default integer type in Rust?

Answer:
The default integer type is i32, because it provides a good balance between performance and memory usage.

  1. What are floating-point types in Rust?

Answer:
Rust has two floating-point types:

f32 (32-bit)

f64 (64-bit, default)

let x = 3.14;      // f64
let y: f32 = 2.5;
Enter fullscreen mode Exit fullscreen mode
  1. What is the bool data type?

Answer:

The bool type represents logical values:

true

false

let is_valid: bool = true;
Enter fullscreen mode Exit fullscreen mode
  1. What is the char data type in Rust?

Answer:
char represents a single Unicode scalar value and occupies 4 bytes.

let c: char = 'A';
let emoji: char = 'πŸ¦€';
Enter fullscreen mode Exit fullscreen mode

Intermediate Level

  1. What are compound data types in Rust?

Answer:
Compound types group multiple values into one type. Rust provides:

Tuples

Arrays
Enter fullscreen mode Exit fullscreen mode
  1. Explain tuples in Rust.

Answer:
Tuples store multiple values of different types in a fixed order.

let person: (&str, u32) = ("Alice", 30);
Enter fullscreen mode Exit fullscreen mode
  1. How do you access tuple elements?

Answer:
Using dot notation or destructuring.

let x = person.0;
let (name, age) = person;
Enter fullscreen mode Exit fullscreen mode
  1. Explain arrays in Rust.

Answer:
Arrays store multiple values of the same type with a fixed length.

let numbers: [i32; 3] = [1, 2, 3];
Enter fullscreen mode Exit fullscreen mode
  1. What is the difference between tuple and array?

MCQs on Rust Data Types (With Answers)

MCQs – Basics

  1. Rust is a ______ typed language.
A. Dynamically
B. Statically
C. Weakly
D. Loosely
Enter fullscreen mode Exit fullscreen mode

βœ… Answer: B

  1. Which is the default integer type in Rust?
A. i64
B. i32
C. u32
D. isize
Enter fullscreen mode Exit fullscreen mode

βœ… Answer: B

  1. Which data type represents a single Unicode value?
A. String
B. char
C. &str
D. u8
Enter fullscreen mode Exit fullscreen mode

βœ… Answer: B

  1. How many bytes does a char occupy in Rust?
A. 1
B. 2
C. 4
D. 8
Enter fullscreen mode Exit fullscreen mode

βœ… Answer: C

MCQs – Scalar Types

  1. Which of the following is NOT a scalar type?
A. Integer
B. Boolean
C. Character
D. Tuple
Enter fullscreen mode Exit fullscreen mode

βœ… Answer: D

  1. Which floating-point type is default in Rust?
A. f32
B. f64
C. f16
D. f128
Enter fullscreen mode Exit fullscreen mode

βœ… Answer: B

  1. Which integer type is unsigned?
A. i32
B. isize
C. u8
D. i128
Enter fullscreen mode Exit fullscreen mode

βœ… Answer: C

MCQs – Compound Types

  1. Which compound type allows mixed data types?
A. Tuple
B. Array
C. Vector
D. HashMap
Enter fullscreen mode Exit fullscreen mode

βœ… Answer: A

  1. Which compound type requires all elements to be the same type?
A. Tuple
B. Array
C. Enum
D. Struct
Enter fullscreen mode Exit fullscreen mode

βœ… Answer: B

  1. Which statement about arrays is correct?
A. Arrays can grow dynamically
B. Arrays have fixed length
C. Arrays allow mixed types
D. Arrays skip bounds checking
Enter fullscreen mode Exit fullscreen mode

βœ… Answer: B

Tricky MCQs

  1. What happens if you access an array index out of bounds?
A. Undefined behavior
B. Runtime panic
C. Compile error
D. Memory corruption
Enter fullscreen mode Exit fullscreen mode

βœ… Answer: B

  1. Which type is best for indexing arrays?
A. i32
B. usize
C. u8
D. f64
Enter fullscreen mode Exit fullscreen mode

βœ… Answer: B

  1. Type inference in Rust happens at:
A. Runtime
B. Compile time
C. Link time
D. Execution end
Enter fullscreen mode Exit fullscreen mode

βœ… Answer: B

  1. Which allows different types in a single variable?
A. Array
B. Tuple
C. Integer
D. Boolean
Enter fullscreen mode Exit fullscreen mode

βœ… Answer: B

  1. Rust prevents many errors related to data types at:
A. Runtime
B. Deployment
C. Compile time
D. Production
Enter fullscreen mode Exit fullscreen mode

βœ… Answer: C

One-Line Interview Summary

Rust data types are strictly enforced at compile time, divided into scalar and compound types, ensuring safety, predictability, and memory correctness.

Top comments (0)