YouTip LogoYouTip

Rust Data Types

# Rust Data Types The basic data types in the Rust language are as follows. ### Integer Integers are categorized by bit length and signedness as follows: | 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 being run. For a 32-bit architecture processor, a 32-bit length integer will be used. Integers can be represented in the following ways: | Base | Example | | --- | --- | | Decimal | 98_222 | | Hexadecimal | 0xff | | Octal | 0o77 | | Binary | 0b1111_0000 | | Byte (only for u8) | b'A' | As you can see, some integers have an underscore in the middle. This design makes it easier for people to judge the approximate value of a large number when entering it. ### Floating-Point Rust supports 32-bit floating-point numbers (`f32`) and 64-bit floating-point numbers (`f64`), just like other languages. By default, `64.0` will represent a 64-bit floating-point number because modern computer processors calculate both types at nearly 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 } ### Mathematical Operations A program segment demonstrating 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 indicates a compound assignment operation. For example: `sum += 1` is equivalent to `sum = sum + 1`. **Note:** Rust does not support **++** and **--**, because these operators appearing before or after a variable can affect code readability and reduce the developer's awareness of variable changes. ### Boolean Type The boolean type is represented by `bool`, and its value can only be `true` or `false`. ### Character Type 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 like Chinese, Japanese, and Korean, as well as even emojis and zero-width spaces as valid `char` values in Rust. The Unicode value range is from U+0000 to U+D7FF and U+E000 to U+10FFFF (inclusive). However, the concept of a "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:** Because Chinese text encoding has two types (GBK and UTF-8), using Chinese strings in programming may lead to garbled characters. This is because the text encoding of the source program and the command line are inconsistent. Therefore, in Rust, strings and characters must use UTF-8 encoding, otherwise the compiler will report an error. ### Compound Types A tuple is a group of data enclosed in a pair of `( )`, which can contain different types 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 a collection of 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; let second = a; // Array access a=123;// Error: array a is immutable let mut a =[1,2,3]; a=4;// Correct
← Os DupOs Close β†’