<> data type

rust Two data type subsets of : Scalar type and compound type

rust It's static language , This shows that rust The type of all variables must be known at compile time

<> one Scalar Types Scalar type

4 scalar type : integer , float , Boolean type , character

<>1. Integer Types integer

Integer types are divided into signed integer and unsigned integer

LengthSignedRange(-2n-1 to 2n-1 - 1)UnsignedRange(0 to 2n - 1)
8-biti8-27 ~ 27 -1 (-128 ~ 127)u80 ~ 28 -1 (0 ~ 255)
16-biti16-215 ~ 215 -1u160 ~ 216 -1
32-biti32-231 ~ 231 -1u320 ~ 232 -1
64-biti64-263 ~ 263 -1u640 ~ 264 -1
archisize Architecture depending on computer type usize Architecture depending on computer type
arch Type isize and usize The type depends on the type of computer on which the program is running : If it is 64 Bit architecture , Then 64 position ; If it is 32 Bit architecture , Then 32 position

In addition to the literal amount of bytes , Type suffixes are allowed for all numeric literal quantities , as 57u8, In addition, it can be used _ As a visual separator . as : 1_000, 100_0000.

Number literalsExample
Byte(u8 only)b’A’

If we are not sure which type of integer to use , commonly rust The best type will be selected by default :i32. This type runs the fastest , even if it is 64 Bit operating system . and usize/isiz It is mainly used in the case of indexing a certain set .

<>2. Floating-Point Types float

rust There are mainly two floating-point types: single precision floating-point and double precision floating-point , namely f32 and f64 Corresponding representative 32 Bit single precision sum 64 Bit double precision .

rust Default use f64, Although the current CPU function 32 Position and 64 Speed and indifference of bit floating point type , however 64 Higher precision of bit energy acquisition .

<>3. The Boolean Type Boolean type

rust The boolean type of is : ture and false

<>4. The Character Type Character type

* char Character type usage Single quotation mark , string The string uses Double quotation marks
* rust The character for is Unicode code .
<> two Compound Type Compound type

<>1. The Tuple Type Tuple type

* rust Create a tuple by separating list values with commas within parentheses , Each element in a tuple can be of a different type .fn main() { let tup: (i32,
f64, u8) = (500, 6.4, 1); }
Type comments are optional .

Tuple unpacking destruct:
fn main() { let tup = (500, 6.3, 1); let(x, y, z) = tup; println!("y is : {}",
y); }
Tuples can also be passed through the . + Access elements of tuples by subscript index :
fn main() { let tup = (500, 6.4, 1); let x = tup.0; let y = tup.1; let z =
tup.2; }
<>2. The Array Type Array type

rust Create an array by separating the list values with commas within the square brackets , Different from tuples , Elements within an array must be of the same type .

Different from other programming languages ,rust The length of the array is fixed , Once the statement is made , The size of the array can no longer be changed .

vector A vector type is a variable array , If you're not sure whether to use arrays or vectors , Vector is recommended , Because once an array is declared , The size is not modifiable .

adopt **[ Indexes ]** Accessing array elements :
fn main() { let a = [1,2,3,4,5]; let x = a[0]; let y = a[1]; }

Cross boundary access of array , The compiler does not check for errors , But the runtime will report an error and exit . When a program attempts to access an element by subscript ,rust Checks if the index is less than the length of the array , If it exceeds the limit, it will report an error and exit . This is rust An embodiment of security . Because other low-level languages may directly access other data in memory if they exceed the subscript .rust The access was blocked , Protected memory security .

©2019-2020 Toolsou All rights reserved,
Non preemptive static priority scheduling algorithm for operating system (C language )Go Language learning notes (GUI programming )XCTF Attack and defense world web Advanced practice _ 2_lottery What's the difference between computer major and training background ?python realization vlookup_ Dry goods I : Why python It's inside vlookup Bubble sort primary springboot2 Separation of front and rear platforms ,token Put in header Pit for verification Python Case conversion of letters ( Two methods )javascript event ( Detailed explanation of zero basis )Unity2019 UIElement note ( ten ) Simple exercise 2