Lesson 6 — Basic Data Types


0 x00 review

In the last section, we looked at variables and constants and learned the difference between them. Also preliminary sense of security in Rust. In this section we’ll look at some of the basic data types that are commonly used. Data types are the basis of Rust as a statically compiled language with strong typing. The common basic data types of Rust are integer, floating point, character, and Boolean.

0x01 Integer Type (Integer)

The following table is a list of common integer types in Rust:

The length of the A 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

Each variant has both signed and unsigned data, with I (short for signed int) for signed and U (short for unsigned int) for unsigned. Each signed variant can store numbers ranging from -(2n-1) to 2n-1-1, where n is the number of digits used by the variant. So i8 can store numbers from minus (27) to 27 -1, which is minus 128 to 127. The unsigned variant can store numbers from 0 to 2n-1, so U8 can store numbers from 0 to 28-1, that is, from 0 to 255. In addition, the ISize and usize types depend on the architecture of the computer on which the program is running: on a 64-bit architecture they are 64-bit, and on a 32-bit architecture they are 32-bit.

The example code is as follows:

	// The default i32 type
    let a = 20;
    // Declare an unsigned integer type declaration
    let b: u32 = 300;
    // Suffix declaration
    let c = 4000u32;
Copy the code
Integer Overflow
    let mut d : i8 = 127;
    d = 128;
Copy the code

The variable d is an integer of type i8, and its value ranges from -128,127. So what happens when you change it to 128? If you try to compile, you will find the following error.

This is called an integer overflow. When Rust is compiling, Rust checks for such problems and causes the program to Panic, the term Panic is used by Rust to indicate that the program exits due to an error. Panic will be discussed in a later section. Here is the first to understand.

0x02 Floating point number (Float)

To put it bluntly, floating point numbers are usually what we call decimals, such as 1.9, -99.99, etc. Floating point numbers are classified into f32 and F64 types.

  • f32Also known as theSingle precision floating point type. There are at least six significant digits after the decimal point.
  • f64Also known as theDouble precision floating point typeIf the type is not specified when the variable is declared, it is Rust’s default floating point type. There are at least 15 significant digits to the decimal point.

The example code is as follows:

	// Default type
    let f1 = 0.0;
    // Type declaration
    let f2: f32 = 54.0;
    // Suffix declaration
    let f3 = 99.999 f32;
Copy the code

The decimal point must exist in Rust when defining floating point numbers. It is the only standard that distinguishes integers from floating point numbers and cannot assign integers to floating point numbers. As shown below, the compiler will prompt an error and the compilation will not pass.

0x03 Boolean type (Bool)

Haha, this is easy. Just two values, either true or false. Generally used in logical judgments. The example code is as follows:

	// By default, no type is declared
    let g = true;
    // Declare the type
    let h : bool = false;
Copy the code

0x04 Character type (Char)

The underlying encoding of Rust is UTF-8, which is different from the ASCII encoding used by common C, C++, Java and other languages. The character data types in Rust include numbers, letters, Unicode, and other special characters. Each character is 4 bytes and is defined by single quotes. A character type represents a Unicode Scalar Value. Unicode encoded scalar values range from U+0000 to U+D7AF (inclusive) and U+E000 to U+10FFFF (inclusive). Learn more about Unicode at Baidu Baike.

The example code is as follows:

// Let I = 'I '; Let j: char = 'j'; // let k = '😃'; // Let l = 'zhong '; // Let m = 'ག';Copy the code

0 x05 summary

In this section, you learned about the common data types of Rust, including integer, floating point, Boolean, and character. The data types in Rust cannot be implicitly converted to each other. But they are still interchangeable, and you need to use other methods, as we’ll see in the next section. Interested can check the information in advance oh ~

0x06 Source code for this section

006/data_type · StudyRust – Code Cloud – Open Source China (gitee.com)

In the next section, learn about literals and common operators, and learn to use Rust for some simple operations.