Rust Data Types

The basic data types in the Rust language are as follows.

Integer

Integer types are referred to as integers. According to bit length and whether they are signed, they are divided into the following categories:

Bit length Signed Unsigned
8-bit i8 u8
16-bit i16 u16
32-bit i32 u32
64-bit i64 u64
128-bit i128 u128
arch isize usize

The isize and usize integer types are used to measure data sizes. Their bit length depends on the target platform they run on. If it is a 32-bit architecture processor, a 32-bit integer length will be used.

There are several ways to represent integers:

Base Example
Decimal 98_222
Hexadecimal 0xff
Octal 0o77
Binary 0b1111_0000
Byte (can only represent u8 type) b'A'

Obviously, some integers have an underscore in the middle. This design makes it easier for people to estimate the value of a number when entering a very large number.

Floating-Point

Like other languages, Rust supports 32-bit floats (f32) and 64-bit floats (f64). By default, 64.0 will represent a 64-bit float, because modern computer processors process both types of floating-point calculations at nearly the same speed, but 64-bit floats have higher precision.

Example

fn main() {
    let x = 2.0; // f64
    let y: f32 = 3.0; // f32
}

Mathematical operations

Use a program to illustrate mathematical operations:

Example

fn main() {
    let sum = 5 + 10; // Addition
    let difference = 95.5 - 4.3; // Subtraction
    let product = 4 * 30; // Multiplication
    let quotient = 56.7 / 32.2; // Division
    let remainder = 43 % 5; // Remainder
}

Adding an = sign after many operators means an operation on itself, for example:

sum += 1Equivalent tosum = sum + 1。

Note:Rust does not support++and--, because when these two operators appear before or after a variable, they affect code readability and reduce the developer's awareness of variable changes.

Boolean

Boolean is represented by bool, and its value can only be true or false.

Character

The character type is represented by char.

Rust's char type is 4 bytes in size and represents a Unicode scalar value. This means it can support non-English characters such as Chinese, Japanese, and Korean characters. Even emojis and zero-width spaces are valid char values in Rust.

Unicode values range from U+0000 to U+D7FF and from U+E000 to U+10FFFF (inclusive). However, the concept of "character" does not exist in Unicode, so your intuition about what a "character" is may not match the concept of a character in Rust. Therefore, it is generally recommended to use strings to store UTF-8 text (non-English characters should appear in strings as much as possible).

Note:Since there are two Chinese text encodings (GBK and UTF-8), using Chinese strings in programming may cause garbled characters. This is because the source program and the command line have inconsistent text encodings. Therefore, in Rust, both strings and characters must use UTF-8 encoding, otherwise the compiler will report an error.

Compound types

A tuple uses a pair of( )parentheses to enclose a set of data, which can contain different kinds of data:

Example

let tup: (i32, f64, u8) = (500, 6.4, 1);
// tup.0 equals 500
// tup.1 equals 6.4
// tup.2 equals 1
let (x, y, z) = tup;
// y equals 6.4

An array uses a pair of[ ]brackets to enclose data of the same type.

Example

let a = [1, 2, 3, 4, 5];
// a is an integer array of length 5

let b = ["January", "February", "March"];
// b is a string array of length 3

let c: [i32; 5] = [1, 2, 3, 4, 5];
// c is an i32 array with a length of 5

let d = [3; 5];
// Equivalent to let d = [3, 3, 3, 3, 3];

let first = a[0];
let second = a[1];
// Array access

a[0] = 123; // Error: array a is not mutable
let mut a = [1, 2, 3];
a[0] = 4; // Correct
Other extensions