Trust reading notes basics 1

Time:2021-9-17

Trust noun and cargo

  • Nouns in trust

    • Code package: Crites
    • Original identifier:I don’t understand
  • What is the difference between macros and functions in trust?

    • The difference between the macro of trust and the form of function call, macro band! Function calls do not have;
  • Cargo in trust

    • Cargo new project name: creates a new project, and the project name is a lowercase underlined link
    • Cargo build: build the project and generate the compiled executable file for development
    • Cargo run: compile and run the project
    • Cargo check: ensure that the project can be compiled
    • Cargo build — release: optimize the compilation project for testing

Fundamentals in trust

variable

  • The function and variable names in the rust code use the snake case specification style.
  • Variables are immutable by default
  • Mut identification is added before the variable to identify the variable
  • The difference between variables and constants is that variables are decorated with let, constants are decorated with const, and mut cannot be decorated. Constants cannot be values calculated at run time
  • hide: hiding is to modify variables with the same name with let multiple times, and the following variables will overwrite the previous variables; Hiding can change the type of previous variables, but mut cannot. (pay attention to garbage collection’s treatment of coverage)

Basic type

Integer scalar

length Signed Unsigned
8-bit i8 u8
16-bit i16 u16
32-bit i32 u32
64-bit i64 u64
arch isize usize

Other scalars

length type describe
32-bit f32 Single precision floating point
64-bit f64 Double precision floating point
32-bit char character
8-bit bool Boolean

Composite type

  • Tuple: types can be different, deconstruction access or point access
  • Array: type phase, fixed length, allocated on the stack

function

Function definition

//FN + function name + (parameter) + - > + return value type + {function body}
fn function_name (parameters) -> type_of_return {
   body;
}

In the function signature, the type of each parameter must be declared; There can be multiple return values, using tuples;

function call

//Function name + () 
function_name ();

control flow

If is similar to other languages

Cycle:

loop + break
fn main() {
    let mut counter = 0;

    let result = loop {
        counter += 1;

        if counter == 10 {
            break counter * 2;
        }
    };

    assert_eq!(result, 20);
}
for
fn main() {
    let a = [10, 20, 30, 40, 50];

    for element in a.iter() {
        println!("the value is: {}", element);
    }
}
while
fn main() {
    let a = [10, 20, 30, 40, 50];
    let mut index = 0;

    while index < 5 {
        println!("the value is: {}", a[index]);

        index = index + 1;
    }
}

Recommended Today

Seven Python code review tools recommended

althoughPythonLanguage is one of the most flexible development languages at present, but developers often abuse its flexibility and even violate relevant standards. So PythoncodeThe following common quality problems often occur: Some unused modules have been imported Function is missing arguments in various calls The appropriate format indentation is missing Missing appropriate spaces before and after […]