HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to RustVariablesData TypesOperatorsStringsVectors (Vec)Conditions (if / match)Loops (for / while)FunctionsOwnership & Borrowing
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Vector & HashMap
🔁 Condition & Loop
🧩 Function
🔑 Ownership & Borrowing
Rust›Data Types

Data Types

i32, f64, bool, char; type casting (as) and type inference.

Simple enough for a 5-year-old to understand

Basic types

Example code
1
fn main() {
2
let number: i32 = 42;
i32 = 32-bit integer.
Variablei32 = 32-bit integer.
3
let decimal: f64 = 1.72;
f64 = floating-point decimal.
Variablef64 = floating-point decimal.
4
let dogru: bool = true;
boolean.
Variableboolean.
5
println!("{} {} {}", number, decimal, dogru);
42 1.72 true
Output / Input42 1.72 true
6
}
VariableOutput / Input
💡 Hint: Even if you don't write the type, Rust usually infers it; but sometimes (e.g., parse) you need to specify it.

About this topic

Rust's type system carries the size in the name: i32 means a thirty-two-bit signed integer, u8 an eight-bit unsigned integer, and f64 a double-precision floating-point number. There is no platform-dependent ambiguity.

Rust does NOT perform IMPLICIT conversion between types. Even assigning an i32 value to an i64 variable is a compilation error; you must write the conversion explicitly. This strictness completely eliminates the silent data loss bugs found in C.

Overflow behavior is also explicitly defined. In a debug build, integer overflow panics the program and prints a message; in a release build, it wraps around. The fact that overflow is not undefined behavior is a major difference between Rust and C.

What you will learn

  • Integer and floating-point types
  • That there is no implicit conversion and conversion using as
  • bool and char types
  • Tuple and array types
  • Overflow behavior

When to use?

Unless there is a specific reason, i32 is used for integers and f64 for floats; these are Rust's default choices and are correct in most cases.

u8 is used when working with byte arrays. usize is used for array indices and lengths; this type matches the platform's address width.

Common mistakes

Wrong

fn main() {
    let a: i32 = 5;
    let b: i64 = a;
    println!("{}", b);
}

Rust does not perform implicit numeric conversion, even when there would be no data loss. The compiler gives an "expected i64, found i32" error. You must write the conversion explicitly with as; this makes where the conversion happens visible to anyone reading the code.

Correct

fn main() {
    let a: i32 = 5;
    let b: i64 = a as i64;
    println!("{}", b);
}

Wrong

fn main() {
    let result = 7 / 2;
    println!("{}", result);
}

The division of two integers is an integer: the result is 3, the decimal part is truncated. Rust behaves the same as C in this regard and does not give a warning. To get a floating-point result, the operands must be written as floats.

Correct

fn main() {
    let result = 7.0 / 2.0;
    println!("{}", result);
}

Wrong

fn main() {
    let letter: char = "A";
    println!("{}", letter);
}

Double quotes produce a string slice, single quotes produce a character; the compiler gives an "expected char, found &str" error. In Rust, a char is four bytes and holds a single Unicode code point — this differs from C's char type, which holds a single byte.

Correct

fn main() {
    let letter: char = 'A';
    println!("{}", letter);
}

Frequently asked questions

What is usize, why a separate type?

It is an unsigned integer at the platform's address width: eight bytes on sixty-four-bit systems. Array indices and lengths are of this type because they must represent the largest value that can be addressed in memory. Therefore, you may need to write conversions in index calculations.

What happens if an overflow occurs?

In a debug build, the program panics and prints the "attempt to add with overflow" message. In a release build, the value wraps around. If you want the behavior to be a conscious choice, you can use methods starting with wrapping or checked.

What are tuples for?

They are used to carry a few values of different types as a single value; it is the shortest way to return multiple results from a function. Elements are accessed with a dot and an index, or unpacked into separate variables using pattern matching.

Summary

Type names include bit width; there is no platform-dependent ambiguity.

There is no implicit numeric conversion; it is written explicitly with as.

Integer division truncates the decimal part.

Overflow is not undefined behavior; it panics or wraps around depending on the compilation profile.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code