Rust study notes a data type

EDITORIAL

I'm not a particularly drastic Daniel, education is very low, only some of the new language more interested, contact with the language is not too much not too little, most also kick the tires, so there may be some bias on the understanding .

He taught himself Java, Kotlin, Python, Rust, Xueyibujing but still summed up the law of a fast start with a new language. Learning a new language routines I summarized as: data types (comprising naming), control statements (loop, logical judgment), the method named, or related object-oriented (inheritance, interfaces, polymorphism if it is object-oriented language), unique syntax sugar.

Rust installation

Official website has very detailed matter, comes with installation method

Personal use is Linux, a command line to get recommended for Windows users to install open WSL Rust, then write code using remote-ssh plugin VScode of.

Why do we need to distinguish data type?

Wrote js, Python students know, named variables when we do not need to specify the data type, in fact, most do not need to specify the type of dynamic languages, however static language, similar to Java, C need to compile such language program is compiled have a good memory space allocated when our variables, you need to specify the type of good data.

Rust data types

Variable name and the variable and non-variable

name

Use keywords letto name variables

fn main() {
    let a=5;
}

Variable and immutable

fn main() {
    let a=5;
    //变量默认情况下,不可变
    a=6;
    //这样写会报错,因为我们命名了一个不可变的变量a
    //之后又修改了a的值为6
}

rust default, all variables are immutable, if required variable variable, you need to use let mutto name the variable
mutis the word mutableabbreviation.

let mut a=5;
a=6;
//这样写是不会报错的。

Integer

Next we look at the data types of the integer Rust.

Integer naming: signed - i(bit)unsigned -u(bit)

Baidu: a byte (byte) of 8 bits (bit)

The 8bit signed integer: i8

32bit unsigned integer: u32

bitValues ​​can be used are: 8,16,32,64,128, size

size: If the computer is 32 bits, then isizeto i3264-bit wasi64

let a:i32=5;

But we just variability, and did not specify the type of a variable ah?

This is because if given specific values in Rust is not possible to specify a specific type, the compiler will go down to his type.
Well let a=3written, how much his type would it be?
In Rust, the default type is such wording i32, and also suggest that you use i32this type, it is because the compiler for i32this particular type of optimization done.
On the operation i32will be faster than other data types.

of course! Rust as an underlying language, the speed has been very quickly.

floating point

That is, we often say that decimal. With respect to the integer, decimal, more than a decimal point ., and the position of the decimal point and are not sure where, it seemed as if floating in the same number in the bunch, so we as a decimal floating point numbers (floating-point numbers).
Integer analogy to learn, there are two floating-point, single-precision floating-point f32and double-precision floating pointf64

fn main(){
    let a=3.14;//a的类型为f64
    let b:f32=3.14;//b的类型为f32
}

Rust default floating-point type is f64because their calculation speed almost as fast, but f64higher accuracy.

Boolean

Write tomorrow

character

String

Tuple

Array

Guess you like

Origin www.cnblogs.com/b612/p/12004634.html