RUST learning diary Lesson 7 - literals and operators

Posted by nyy2000 on Sun, 23 Jan 2022 04:46:39 +0100

RUST learning diary Lesson 7 - literals and operators

0x00 review

The previous section explained variables and constants, mainly to understand their differences. I also initially feel the need for security in Rust. In this section, we will explain literal values and some common operators.

0x01 Literal

Literal quantity is a representation used to express a fixed value in the source code. Almost all computer programming languages have literal representations of basic values, such as integers, floating-point numbers, and strings. Literal quantities can only appear as values to the right of the equal sign.

The example code is as follows:

    // Literal 
    // (1) The type is represented by a suffix
    let a = 7u8;
    let b = 5.4f64;
    let c = 32i32;
    let d = "zhangsan";

    // (2) Base is represented by a prefix
    // Binary
    let e = 0b1;
    // octal number system
    let f = 0o7;
    // hexadecimal
    let g = 0xa;
    // byte
    let h = b'A';

    // (3) By "" To segment data, easy to read
    let j = 10_000;
    let k = 200_000.000_1;

As can be seen from the above example, in Rust, prefixes are used to represent hexadecimal types, such as 0b, 0o, 0x and B respectively represent binary, octal, hexadecimal and byte types. You can also declare the type of literal by using suffix, if let a = 7u8. Pass_ To segment numeric literals, easy to read. 10_ 000 equals 10000.

0x02 arithmetic operator

When it comes to operators, the author instantly thought of his childhood. Sitting in the primary school classroom, the teacher explained the mixed four operations on the blackboard Far away. Operators exist in all programming languages, and Rust is no exception.

nameoperatorexplain
plus+Addition operation
reduce-Subtraction operation
ride*Multiplication
except/Division operation
Surplus%Remainder operation
    let aa = 12;
    let bb = 9;
    dbg!(aa + bb);
    dbg!(aa - bb);
    dbg!(aa * bb);
    dbg!(aa / bb);
    dbg!(aa % bb);
    // Self determination and self subtraction operators are not supported
    // aa++ bb++

Code execution result:

[src\main.rs:34] aa + bb = 21
[src\main.rs:35] aa - bb = 3
[src\main.rs:36] aa * bb = 108
[src\main.rs:37] aa / bb = 1
[src\main.rs:38] aa % bb = 3

In addition, Rust does not support self increasing and self decreasing operations like C, C + +, Java and other languages. However, the following operations are supported.

nameoperatorexplain
Plus equals+=Add it first and assign it to the variable on the left
Minus equals-=Subtract first and assign it to the variable on the left
Multiply equal*=First multiply and assign to the variable on the left
Division equals/=First divide and assign to the variable on the left
Remainder equals%=Take the remainder and assign it to the variable on the left
    let mut cc = 20;
    let dd = 5;

    println!("Initial value cc = {} dd = {}", cc, dd);
    // cc = cc + dd;
    cc += dd;
    println!("after cc += dd After the operation, cc The value of is:{:}", cc);
    // cc = cc - dd;
    cc -= dd;
    println!("after cc -= dd After the operation, cc The value of is:{:}", cc);
    // cc = cc * dd;
    cc *= dd;
    println!("after cc *= dd After the operation, cc The value of is:{:}", cc);
    // cc = cc / dd;
    cc /= dd;
    println!("after cc /= dd After calculation, cc The value of is:{:}", cc);
    // cc = cc % dd;
    cc %= dd;
    println!("after cc %= dd After the operation, cc The value of is:{:}", cc);

    // Self increasing
    cc += 1;
    println!("after cc += 1 After the operation, cc The value of is:{:}", cc);
    // Self subtraction
    cc -= 1;
    println!("after cc -= 1 After the operation, cc The value of is:{:}", cc);

Code execution result:

Initial value cc = 20 dd = 5
 after cc += dd After calculation, cc The value of is:25
 after cc -= dd After the operation, cc The value of is:20
 after cc *= dd After the operation, cc The value of is:100
 after cc /= dd After calculation, cc The value of is:20
 after cc %= dd After the operation, cc The value of is:0
 after cc += 1 After the operation, cc The value of is:1
 after cc -= 1 After calculation, cc The value of is:0

After reading the above code, it is actually equivalent to the abbreviation of operation. For example, a+=b is equivalent to a = a + b. Self increasing and self decreasing can also be realized by the above methods.

0x03 relational operator

The relational operator is used to compare the relationship between two, and the return value is a boolean type. The following table lists all the relational operators.

nameoperatorexplain
greater than>If the left operand is greater than the right operand, it returns true; otherwise, it returns false
less than<If the left operand is less than the right operand, it returns true; otherwise, it returns false
be equal to==If the left operand is equal to the right operand, it returns true; otherwise, it returns false
Greater than or equal to>=If the left operand is greater than or equal to the right operand, it returns true; otherwise, it returns false
Less than or equal to<=If the left operand is less than or equal to the right operand, it returns true; otherwise, it returns false
Not equal to!=If the left operand is not equal to the right operand, it returns true; otherwise, it returns false

The example code is as follows:

    let mm = 15;
    let nn = 30;

    dbg!(mm > nn);
    dbg!(mm < nn);
    dbg!(mm == nn);
    dbg!(mm >= nn);
    dbg!(mm <= nn);
    dbg!(mm != nn);

Code execution result:

[src\main.rs:74] mm > nn = false
[src\main.rs:75] mm < nn = true
[src\main.rs:76] mm == nn = false
[src\main.rs:77] mm >= nn = false
[src\main.rs:78] mm <= nn = true
[src\main.rs:79] mm != nn = true

0x04 logical operator

Logical operators are used to combine two or more conditional expressions that return Boolean results. Logical operators are often called "and", "or" and "not" in mathematics books. Strictly speaking, it should be called logical and, logical or, logical non. See the table below for details.

nameoperatorexplain
Logic and&&If the conditional expressions on both sides are true, it returns true; otherwise, it returns false
Logical or||If one of the conditional expressions on both sides is true, it returns true; otherwise, it returns false
Logical non!Returns false if the expression is true, otherwise returns true

The example code is as follows:

    let rr = 56;
    let ss = 34;

    dbg!(rr > 40 && ss > 40);
    dbg!(rr > 40 || ss > 40);
    dbg!(!(rr > 40));

Code execution result:

[src\main.rs:90] rr > 40 && ss > 40 = false
[src\main.rs:91] rr > 40 || ss > 40 = true
[src\main.rs:92] !(rr > 40) = false

0x05 bit operator

Bitwise operators operate on binary data. The following table lists all bit operations supported by Rust.

nameoperatorexplain
Bit and&If the same bit is 1, it returns 1, otherwise it returns 0
Bit or|As long as one of the same bits is 1, it returns 1, otherwise it returns 0
XOR^If the same bits are different, it returns 1, otherwise it returns 0
Bit non!Replace 1 in the bit with 0 and 0 with 1
Shift left<<All bits in the operand are moved to the left by the specified number of bits, and the bits on the right are supplemented by 0
Shift right>>All bits in the operand move to the right by the specified number of bits, and the left bit is supplemented by 0

Take the numbers 2 and 7 as examples to see the running results of the code. The example code is as follows:

    // Binary representation of 2
    let x: i8 = 0b00000010;
    // Binary representation of 7
    let y: i8 = 0b00000111;

    println!("{:08b} & {:08b} = {:08b}", x, y, x & y);
    println!("{:08b} | {:08b} = {:08b}", x, y, x | y);
    println!("{:08b} ^ {:08b} = {:08b}", x, y, x ^ y);
    println!("!{:08b} = {:08b}", x, !x);
    println!("{:08b} << 1 = {:08b}", x, x << 1);
    println!("{:08b} >> 1 = {:08b}", x, x >> 1);

PS: in the code, {: 08b} is to convert the data into binary format for output, with a total of 8 bits. The unknown blank part is filled with 0. The contents of formatted output will be introduced in the following chapters. You can learn about it here.

Code execution result:

00000010 & 00000111 = 00000010
00000010 | 00000111 = 00000111
00000010 ^ 00000111 = 00000101
!00000010 = 11111101
00000010 << 1 = 00000100
00000010 >> 1 = 00000001

0x06 summary

Arithmetic operators have been learned. Have you ever thought about doing a problem exercise? Please use Rust language to answer the following questions:

Xiao Ming has 5 yuan and Xiao Hong has 3.5 yuan. How much do Xiao Ming and Xiao Hong have in total?

0x07 source code of this section

007 · StudyRust - Code cloud - Open Source China (gitee.com)

Next section preview -- learn about Rust's type conversion