English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Rust Data Types

The basic data types in Rust are as follows.

Integer type (Integer)

Integer types are abbreviated as integer types and are divided into the following types according to bit length and unsigned/signed:

bit lengthsignedunsigned
8-biti8u8
16-biti16u16
32-biti32u32
64-biti64u64
128-biti128u128
archisizeusize

isize and usize are two integer types used to measure data size, and their bit length depends on the target platform being run, if it is 32 bit architecture processors will use 32 bit-by-bit length integer type.

There are several ways to express integers:

radixExample
decimal98_222
hexadecimal0xff
octal0o77
binary0b1111_0000
bytes (which can only represent u8 type)b'A'

It is obvious that there is an underscore in the middle of some integers. This design makes it easier for people to judge the value of a large number when entering it.

Floating point type (Floating-Point)

Rust, like other languages, supports 32 bit floating-point numbers (f32) and 64 bit floating-point numbers (f64). By default,64.0 will represent 64 bit floating-point numbers, because modern computer processors calculate both floating-point numbers at almost the same speed, but 64 bit floating-point precision is higher.

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

Mathematical operations

Use a piece of code to reflect mathematical operations:

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; // Modulo
}

Many operators followed by = are meant to be self-operations, for example:

sum += 1 is equivalent to sum = sum + 1.

Note:Rust does not support ++ and --because these two operators appear before and after variables, which can affect the readability of the code and weaken the developer's awareness of variable changes.

Boolean type

Boolean types are represented by bool, and the value can only be true or false.

Character type

Character types are represented by char.

The size of Rust's char type is 4 A byte, representing a Unicode scalar value, means that it can support non-English characters such as Chinese, Japanese, Korean characters, as well as emojis and zero-width spaces, which are all valid char values in Rust.

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

Note:Since there are two types of Chinese text encoding (GBK and UTF-8Therefore, using Chinese strings in programming may cause garbled characters to appear, because the source program and the command line text encoding are inconsistent, so in Rust, strings and characters must use UTF-8 Encoding, otherwise the compiler will report an error.

Composite Type

A tuple is a pair of ( ) that includes a set of data, which can include different types of data:

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

An array is a pair of [ ] that includes homogenous data.

let a =[1, 2, 3, 4, 5];
// a is a length of 5 integer array
let b =["January", "February", "March"];
// b is a length of 3 string array
let c: [i32; 5] =[1, 2, 3, 4, 5];
// c is a length of 5 of the i32 Array
let d =[3; 5];
// Is 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 immutable
let mut a =[1, 2, 3];
a[0] = 4; // Correct