codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🦀

Rust

6 / 58 topics
1Getting Started with Rust2Installing Rust3Hello World Program4Rust Syntax5Variables and Mutability6Data Types7Operators
Tutorials/Rust/Data Types
🦀Rust

Data Types

Updated 2026-05-15
10 min read

Data Types

Introduction

Rust is a statically typed programming language, which means that the type of every variable must be known at compile time. This helps catch errors early and makes Rust programs more reliable. In this section, we will explore the basic data types available in Rust, including scalar types (such as integers, floating-point numbers, booleans, and characters) and compound types (such as tuples and arrays).

Concept

Scalar Types

Scalar types represent a single value. Rust has four primary scalar types: integers, floating-point numbers, booleans, and characters.

Integers

Integers are whole numbers without a fractional component. Rust provides several integer types with varying sizes:

  • i8, i16, i32, i64, i128: Signed integers (can be negative).
  • u8, u16, u32, u64, u128: Unsigned integers (cannot be negative).

The default integer type is i32 because it provides a good balance between size and performance on most systems.

Floating-Point Numbers

Floating-point numbers are numbers with a fractional component. Rust has two floating-point types:

  • f32: Single-precision floating point.
  • f64: Double-precision floating point (default).

Booleans

Booleans represent true or false values. The type is written as bool.

Characters

Characters in Rust are represented by the char type, which is four bytes in size and represents a Unicode Scalar Value.

Compound Types

Compound types can group multiple values into one type. Rust has two primitive compound types: tuples and arrays.

Tuples

A tuple groups a number of values with a variety of types into one compound type. Tuples have a fixed length:

let tup = (500, 6.4, 'z');

You can access the elements of a tuple using pattern matching or dot notation:

let (x, y, z) = tup;
println!("The value of y is: {}", y);

Or:

let five_hundred = tup.0;
let six_point_four = tup.1;
let z = tup.2;

Arrays

An array in Rust has a fixed size and can store elements of the same type:

let a = [1, 2, 3, 4, 5];

You can access elements of an array using indexing:

let first = a[0];
let second = a[1];

Examples

Integer Types

Here's an example demonstrating the use of different integer types:

Rust
1fn main() {
2 let x: i8 = 5;
3 let y: u32 = 42;
4 println!("x is {} and y is {}", x, y);
5}

Floating-Point Numbers

Here's an example of using floating-point numbers:

Rust
1fn main() {
2 let a = 2.0; // f64
3 let b: f32 = 3.14; // f32
4 println!("a is {} and b is {}", a, b);
5}

Booleans

Here's an example of using booleans:

Rust
1fn main() {
2 let t = true;
3 let f: bool = false; // with explicit type annotation
4 println!("t is {} and f is {}", t, f);
5}

Characters

Here's an example of using characters:

Rust
1fn main() {
2 let c = 'z';
3 let z: char = 'ℤ'; // with explicit type annotation
4 println!("c is {} and z is {}", c, z);
5}

Tuples

Here's an example of using tuples:

Rust
1fn main() {
2 let tup = (500, 6.4, 'z');
3 let (x, y, z) = tup;
4 println!("The value of x is: {}", x);
5 println!("The value of y is: {}", y);
6 println!("The value of z is: {}", z);
7}

Arrays

Here's an example of using arrays:

Rust
1fn main() {
2 let a = [1, 2, 3, 4, 5];
3 let first = a[0];
4 let second = a[1];
5 println!("The first element is {} and the second element is {}", first, second);
6}

What's Next?

In the next section, we will explore operators in Rust, which allow you to perform operations on data types. Understanding operators is crucial for performing calculations and manipulating data in your programs.

Stay tuned!


PreviousVariables and MutabilityNext Operators

Recommended Gear

Variables and MutabilityOperators