Basics

Rust Data Types

Rust Data Types

Rust data types include i32 f64 and String with strict typing.

Introduction to Rust Data Types

Rust is a statically typed language, which means that the data types of all variables must be known at compile time. This leads to safer and more predictable code. In Rust, data types are primarily categorized into two groups: scalar and compound types.

Scalar Types

Scalar types represent a single value. Rust's scalar types include integers, floating-point numbers, characters, and booleans.

Integer Types

Rust provides several integer types with different sizes. These types include i8, i16, i32, i64, i128 for signed integers, and u8, u16, u32, u64, u128 for unsigned integers. The number in the type name indicates the bit size of the integer.

Floating-Point Types

Rust has two floating-point types: f32 and f64. These types are used for decimal and fractional numbers. By default, Rust uses f64 because it is more precise.

Boolean Type

The boolean type in Rust is bool and can have one of two values: true or false. Booleans are often used in control flow statements.

Character Type

The char type represents a single Unicode Scalar Value, which means it can represent a wide range of characters from different languages and scripts.

Compound Types

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

Tuples

Tuples can store a fixed number of values of varying types. They are useful for returning multiple values from a function.

Arrays

Arrays in Rust can hold multiple values of the same type. Arrays have a fixed length, which must be known at compile time.

String Type

Strings in Rust are more complex than the basic scalar and compound types. They are stored as a sequence of UTF-8 encoded bytes. Rust provides two main types for strings: &str (string slices) and String (owned strings).

Previous
Variables