Rust looks for the basic data type of frustration 1.0

Time:2021-6-7

When we come into contact with a new language, we should first understand its related basic concepts, so today we will talk about the data types of rust. Basic typists can roughly compare with known systems.

Each value in rust has a data type, so rust will know which processing method this value is; Generally speaking, there are two types: scalar type and compound type. In addition, rust requires that the types of all variables must be known at compile time.

Scalar type

Integer type, floating point type, boolean type and character type are the four scalar types of rust, and they are also the most basic types. If there are other basic language types, they can be omitted.

integer

Integer is the number type without decimal (integer type). The default of integer is I32. I is a signed integer type and 32 is a 32-bit length. Sign and no sign, that is, whether the number can be negative, each variant can be signed or no sign. All integer types are listed below:

Signed No sign Occupied length
i8 u8 8 bytes
i16 u16 16 bytes
i32 u32 32 bytes
i64 u64 64 bytes

In addition, rust allows the use of_It is used as a divider to recognize the numeric literal value. as2_000. You can also use type suffixes to declare numeric values, except for all numeric values unexpected by byte type, such as50u8

Next, let’s sort out several ways to declare integers

fn main() {
    let x = 2000; // i32
    
    let x = 2000i32;
    
    let x = 2_000;
    
    let x: i32 = 2000;
}

floating-point

Floating point is simply a number with a decimal point (floating point) type. The float type in rust isf32andf64They are 32-bit and 64 bit, respectively.

So we give a case directly

fn main() {
    let x = 5.0;
    
    let x: f32 = 5.0;
    
    let x = 5.0f32;
}

Boor

We usually use Boolean values in conditional expressions. They are only yes or no. the boolean type in rust has two possible values: true and false. useboolexpress. Direct code:

fn main() {
    let x = true;
    
    let x: bool = true;
}

character

Character typecharIs a native letter type in a language, specified by single quotation marks. When it comes to characters, we must mention string. There is only one string type in ruststr, string slice, which can be understood as a collection of bytes plus strings implemented by some methods. This concept will be described in detail later. Let’s first look at how to use characters:

fn main() {
    let x = 'z';
    let x = 'Y';
    let x = '?';
}

Compound type

There are two native composite types in rust: ancestor and array, which can combine multiple values into one type.

array

The array contains multiple values, and each element must have the same type and fixed length; Once declared, the length cannot be changed. This is different from other languages. Array data is stored on the stack. In the rust standard library, the collection type vector similar to array is on the heap. You can do your homework first.

The following code shows two ways to store a week’s name in an array:

fn main() {
    let weeks = [
        "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", 
        "Saturday", "Sunday"
    ];
    
    let weeks: [&str; 7] = [
        "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", 
        "Saturday", "Sunday"
    ];
}

So how do we access the elements of an array? As mentioned above, an array is a block of memory allocated on the stack. Then we can access the array elements through the index. We use the index to access the above Wednesday and Thursday elements

fn main() {
    let weeks = [
        "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", 
        "Saturday", "Sunday"
    ];
    
    let wed = weeks[2];
    let thurs = weeks[3];
    //Small white tip: computer index is from 0 to start Oh
}

Of course, if we access the index value outside the array, it will cause a run errorindex out of bounds

tuple

Unlike arrays, Yuanzu allows different types to be combined, and each element has a type. As follows:

fn main() {
    let rectangle:(i32,f64) = (200, 6.4);
}

What’s more complicated is that we need to use pattern matching to deconstruct tuple values or to use pattern matching.This is followed by the index value so that a single value can be obtained from the tuple.

fn main() {
    let rectangle:(i32,f64) = (200, 6.4);
    
    let height = x.0;
    let width = x.1;
    
    let (height, width) = rectangle;
}

Small blackboard

We know the basic data types in rust and how to use them. Here is a small blackboard to supplement the numerical operation of number type+、-、*、/

fn main() {
    //Plus
    let plus = 1 + 1;

    //Minus
    let minus = 2 - 1;

    //By
    let multiply = 1 * 2;

    //Except
    let divide = 2 / 1;

    //Surplus
    let remainder = 3 % 2;
}

class is over

Many typists may feel more basic when they see this, and they don’t feel any frustration. These articles are completely based on the growth path of typists learning rust. While sharing, they also help typists sort out the basic theoretical knowledge. It doesn’t matter if you don’t feel the frustration. I’m looking forward to the storm( flee