Menu Close

Rust – Data types

In this article, we are going to explore the core Rust data types in detail, along with their usage and examples.

Rust data types can be broadly classified into two categories: primitive, compound and User-Defined types. Primitive data types are the building blocks for more complex data structures, while compound data types combine multiple values into one.

Table of Contents

Primitive (Scalar) Types

Integer Types:

Integers are whole numbers that can be either signed (with a +/- sign) or unsigned (without a sign). Rust provides various integer types with different sizes: i8, i16, i32, i64, i128, u8, u16, u32, u64, and u128. The number following the i or u denotes the number of bits the integer occupies.

Signed integers: i8, i16, i32, i64, i128, isize

Unsigned integers: u8, u16, u32, u64, u128, usize

Example:

fn main() {

    let a: i32 = -100;

    let b: u32 = 100;

    println!("Signed Integer: {}", a);

    println!("Unsigned Integer: {}", b);

}

Floating-Point Types:

Rust has two floating-point types: f32 and f64. They represent single-precision and double-precision IEEE-754 floating-point numbers, respectively. By default, Rust uses f64 for floating-point literals as it offers more precision and is almost as fast as f32 on modern CPUs.

Floating-point numbers: f32 (single-precision), f64 (double-precision)

Example:

fn main() {
    
    let a: f32 = 3.14;

    let b: f64 = 2.718281828;

    println!("Single-precision float: {}", a);

    println!("Double-precision float: {}", b);

}

Boolean Type:

A Boolean value can be either true or false and is represented by the bool data type in Rust.

Boolean values: bool (true or false)

Example:

fn main() {

    let a: bool = true;

    println!("Boolean value: {}", a);

}

Character Type:

A character in Rust is represented by the char data type, which holds a Unicode scalar value. Characters are enclosed in single quotes, while strings are enclosed in double quotes.

Unicode scalar values: char

fn main() {

    let a: char = 'A';

    println!("Character value: {}", a);

}

Compound Types

Compound types group multiple values into a single type. Rust has two primary compound types: tuples and arrays.

Tuples

A tuple is a collection of values with different types, grouped together. The tuple has a fixed length, which cannot be changed after declaration.

Example:

fn main() {

    let person: (i32, &str, f64) = (30, "Alice", 68.5);

    println!("Age: {}, Name: {}, Weight: {}", person.0, person.1, person.2);

}

Arrays

An array is a collection of values with the same type, stored in contiguous memory. Arrays have a fixed size, which is determined at compile time.

Example:

fn main() {

    let numbers: [i32; 5] = [1, 2, 3, 4, 5];

    println!("Array values: {:?}", numbers);

}

User-Defined Types

Rust allows users to create their custom data types using the following constructs:

Struct:

A struct (short for “structure”) is a composite data type that groups related values of different types under a single name. Structs are defined using the “struct” keyword, and their members are accessed using dot notation.

Example:

struct Point {

    x: i32,

    y: i32,

}


fn main() {

    let point = Point { x: 3, y: 4 };

    println!("Struct values: ({}, {})", point.x, point.y);

}

Enum:

An enum (short for “enumeration”) is a data type that represents a set of named values, called variants. Enums are defined using the “enum” keyword and can be used to create more expressive and type-safe code.

Example:

enum Direction {

    North,

    South,

    East,

    West,
}


fn main() {

    let direction = Direction::North;

    match direction {

        Direction::North => println!("Heading North"),

        Direction::South => println!("Heading South"),

        Direction::East => println!("Heading East"),

        Direction::West => println!("Heading West"),

    }

}

Union (less commonly used due to unsafe nature):

Unions in Rust are similar to unions in C, where a union can store multiple types of data in the same memory location. Unions are defined using the “union” keyword. They are less commonly used in Rust due to their unsafe nature, as they require manual memory management.

Example:

union MyUnion {

    f1: u32,

    f2: f32,

}


fn main() {

    let mut my_union = MyUnion { f1: 1 };

    unsafe {

        println!("Union value as u32: {}", my_union.f1);

        my_union.f2 = 2.0;

        println!("Union value as f32: {}", my_union.f2);

    }

}

Conclusion

Understanding the different data types in Rust is essential for writing efficient and safe code. Rust provides a rich set of built-in data types, including primitive types, compound types, and user-defined types. By mastering these data types, developers can harness Rust’s powerful type system to create robust and high-performance applications.

To check more Go related articles. Pls click given below link:

https://www.techieindoor.com/rust-a-deep-dive-into-rust-with-examples/

https://en.wikipedia.org/wiki/Rust_(programming_language)

Posted in Rust

Leave a Reply

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