Rust Data Types

Rust Tutorial

Rust Data Types

Rust Data Types is a statically typed language, which means every variable has a known type at compile time. Rust data types are broadly divided into Scalar and Compound types.

 1. Scalar Data Types

Scalar types represent single values.

 Integer Types

TypeSizeExample
i8 / u88-bitlet a: i8 = -10;
i16 / u1616-bit
i32 / u3232-bit (default)let b = 20;
i64 / u6464-bit
i128 / u128128-bit
isize / usizearch dependent

 Floating-Point Types

TypeSize
f3232-bit
f6464-bit (default)

 Boolean Type


 Character Type

✔ Rust char is 4 bytes (Unicode).


 2. Compound Data Types

Compound types group multiple values into one type.


Tuple

Fixed-size collection of different data types.

Destructuring:


 Array

Fixed-size collection of same data type.


 3. String Types (Very Important)

&str (String Slice)

  • Immutable

  • Stored in read-only memory


String (Heap Allocated)


 4. Custom Data Types

 Struct


 


 Enum


 


 5. Option Type (No Null in Rust)

Rust has no null. Instead:


 6. Result Type (Error Handling)


 Type Inference Example


 Important Notes

  • Rust prevents invalid memory access

  • No implicit type conversion

  • Use as for explicit casting

  • Safer than C/C++ data handling

You may also like...