Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Rust Data Types

Rust Data Types

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

Integer

The integer type, or integer for short, is divided into the following categories according to bit length and signedness:

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 size. Their bit length depends on the target platform on which they run; on a 32-bit architecture processor, 32-bit-length integers will be used.

Integers can be expressed in the following ways:

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

Obviously, some integers contain an underscore in the middle. This design makes it easier for people to judge approximately what value a very large number is when entering it.

Floating-Point

Rust, like other languages, supports 32-bit floating-point numbers (f32) and 64-bit floating-point numbers (f64). By default, 64.0 represents a 64-bit floating-point number, because modern computer processors compute both floating-point types at almost the same speed, but 64-bit floating-point numbers have higher precision.

Example

fn

main

(

)

{

   

let

x

=

2.0

;

// f64

   

let

y

:

f32

=

3.0

;

// f32

}

Math operations

A program to reflect 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

;

// Modulo

}

Adding an = sign after many operators means self-operation, for example:

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

**Note:** Rust does not support ++ and --because these two operators appearing before or after a variable affect code readability and weaken developers' awareness of variable changes.

Boolean

The boolean type 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, and even emoji and zero-width spaces are valid char values in Rust.

Unicode values range from U+0000 to U+D7FF and 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 encodings for Chinese text (GBK and UTF-8), using Chinese strings in programming may lead to garbled text, because the source program and the command line use inconsistent text encodings. Therefore, in Rust, strings and characters must use UTF-8 encoding, otherwise the compiler will report an error.

Composite types

A tuple is a set of data enclosed in a pair of ( ), 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 is data of the same type enclosed in a pair of [ ].

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 of length 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