Rust Syntax & Basic Constructs
Estimated reading: 3 minutes 26 views

🦀 Rust – Data Types: Understand the Building Blocks of Rust Programs

🧲 Introduction – Why Learn Rust Data Types?

Rust is a statically typed language, which means every variable has a known type at compile time. Rust’s data type system is designed for performance and safety, allowing fine-grained control over memory and operations—critical for systems-level programming.

🎯 In this guide, you’ll learn:

  • The two main categories: Scalar and Compound types
  • How to declare and use different data types
  • Numeric types, booleans, characters, tuples, and arrays
  • Type inference and explicit typing rules

🧮 Categories of Rust Data Types

Type CategoryExamplesDescription
Scalari32, f64, char, boolRepresents a single value
Compoundtuple, arrayGroups multiple values into one type

🔢 Scalar Data Types in Rust

✅ 1. Integer Types

fn main() {
    let a: i32 = -10;
    let b: u8 = 255;
    println!("a = {}, b = {}", a, b);
}
TypeSizeSigned/UnsignedRange
i88-bitSigned-128 to 127
u88-bitUnsigned0 to 255
i3232-bitSigned (default)-2,147,483,648 to 2.1B
u6464-bitUnsigned0 to 18.4 quintillion

🧠 Rust defaults to i32 if you don’t specify a type.


✅ 2. Floating-Point Types

fn main() {
    let x = 2.5;       // f64 by default
    let y: f32 = 3.14; // explicitly f32
    println!("x = {}, y = {}", x, y);
}
TypeSizeDescription
f3232-bitSingle-precision
f6464-bitDouble-precision (default)

✅ 3. Boolean Type

fn main() {
    let is_rust_fun: bool = true;
    println!("Is Rust fun? {}", is_rust_fun);
}
TypeValues
booltrue or false

✅ 4. Character Type

fn main() {
    let letter: char = 'R';
    let emoji: char = '🦀';
    println!("{} {}", letter, emoji);
}
TypeDescription
char4-byte Unicode scalar value

🧳 Compound Data Types in Rust

✅ 1. Tuples

fn main() {
    let person: (&str, i32) = ("Alice", 30);
    println!("Name: {}, Age: {}", person.0, person.1);
}

📌 Tuples group values of different types, and are accessed using dot notation (.0, .1, etc.).


✅ 2. Arrays

fn main() {
    let scores: [i32; 3] = [85, 90, 95];
    println!("Top score: {}", scores[2]);
}

📌 Arrays hold multiple values of the same type and have fixed size.

SyntaxMeaning
[type; size]Array of specified type and size
let arr = [0; 5];Fills array with 5 zeros

🔄 Type Inference and Annotations

Rust often infers types:

let x = 42; // inferred as i32

But you can add explicit annotations for clarity:

let x: u8 = 42;

📦 When in doubt, explicit typing helps with clarity and compiler errors.


⚠️ Common Data Type Mistakes

MistakeError TypeSolution
Type mismatchCompile errorEnsure operations use same types
Overflow without checkingPanic in debug modeUse correct types or wrapping_*()
Indexing array out of boundsRuntime panicAlways check bounds before indexing

📌 Summary – Recap & Next Steps

Rust’s strong and explicit type system gives you predictable, performant, and bug-free code. Knowing how and when to use different data types is foundational to mastering Rust.

🔍 Key Takeaways:

  • Rust has scalar types (i32, f64, bool, char) and compound types (tuple, array)
  • Type safety and inference help prevent logic errors
  • Always annotate types when clarity or correctness is needed
  • Rust provides full Unicode support via char and precise memory use with array sizing

⚙️ Next up: Deep dive into Rust – Strings to learn about string slices, ownership, and formatting.


❓FAQs


What is the default integer type in Rust?
i32 is the default for integer literals.


How is a tuple different from an array?
✅ Tuples can hold different types and have named or indexed fields. Arrays are fixed-size and homogeneous.


Is char a single-byte type in Rust?
✅ No. Rust’s char is 4 bytes and represents a Unicode scalar value, not just ASCII.


Can I change array sizes dynamically in Rust?
✅ No. For dynamic, growable arrays, use a Vec<T> instead of a fixed-size array.


Share Now :

Leave a Reply

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

Share

Rust – Data Types

Or Copy Link

CONTENTS
Scroll to Top