Rust basis

A: Compiler

Rust compiler called rustc, similar javac as responsible for compiling the source code into an executable file or library file (.a / .so / .lib / .dll, etc.)

II: core and standard libraries

Rust from the core language and standard libraries make up the core of the library is the basis of the standard library, the library does not depend on the core operating system and network-related libraries, generally written for embedded applications will be used;

# Can be introduced at the top of the module [no_std] to use the core library!;

Standard libraries are generally used to develop applications, and it provides the basis for cross-platform support required to develop applications, such as println! Macro is actually a standard library, it depends on the API provided by the operating system to achieve the standard output stream in print data;

Three: Package Manager

Rust package manager is cargo carriage [], and every item in Rust compiled into a library called the crate, similar to Java in a jar;

In addition to a cargo package manager, it can also be used to create an executable program and project library project, commands are:

1.cargo new exec_proj

2.cargo new --lib lib_proj

You can then compile the project through the cargo build and cargo run and allowed;

Four: Rust statements and expressions

Rust in grammar can be divided into two categories, namely statements and expressions;

Statement refers to a semicolon, parentheses or flower end of the code, such as the output statement, declaration statement variable declaration statement structure or the like;

Expression refers to expression of no semicolon, if it is a last line of the X block or function, it will automatically block X as a return value or function, such as an expression 1 + 1 is placed last function herein represents the return 2;

Five: variable binding

The default variable is immutable, that is, let a = 3; a = 4; will complain; but can let mut a = 3; to be declared as a variable of variables; will be assigned to a 3, also known as binding; a i32 is essentially a type of a pointer value pointing to the address space 3 is located;

Although the default variables are immutable, but can be variable transfer / move ownership, i.e., let a = M {..}; let b = a; a case transferring ownership of the object M {..} gave b ; [Note, the type of foundation is copy semantics does not move ownership]

Six: Pointer

In fact, Rust in variable names, references, on native pointers and smart pointers are essentially points to the start address of an address space [But this also has a pointer to the data corresponding to the size of the address space, space, etc. in Rust compilers years, but it compiled will automatically decomposed into different positions of assembly code operations];

Native pointer can let a = 3; let ptr = & mut a as * mut i32; acquiring primary pointer [native pointer has a variable and non-variable native pointer, where * mut i32 is variable, * const i32 is immutable the variable spatial representation may modify its contents pointed to by * ptr = 8; achieved, i.e. native dereferencing pointers (pointers solution) is used] *

Variables can also be achieved by shielding the same scope variables the same variables are defined, such as let a = 3; let a = "kkk" .to_string ();

Its role is generally such let a = Some (44); let a = a.unwrap ();

Seven: functions, methods and closures

The difference between functions and methods are typically a function of the global function is called, and the function impl blocks belonging to a structure called methods, such AClass the test method (static or instance methods also can be divided, the first parameter is example of a method or & self & mut self);

Declare functions and methods are declared fn key;

Closure is essentially anonymous achieve a structure like the trait of Fn;

And closures can function as a function of parameters or return values ​​(pointer function fn (i32, i32) -> i32), except that the function can not capture the context variable;

Rust One const function, i.e., the function can be used as a constant whose value is determined during compilation, which is the basis to achieve Rust reflection, it is written const fn lala () -> i32 {..}

Functions can be declared within the function;

Eight: Process Control

Conditional expression: other different languages, Rust IF expression can return values, but note that the type of return value must be the same in different branches, such as let a = if (...) {3} else {4}

Loop expression: may be used for i in 0..10 {..} achieve similar language C for (int i = 0; i <10; i ++) {..}, which may be in the range of parameter types (though 0..10 should also be the type of short range, less brackets);

There circulation loop {...}} [true constant and cycle while boolean {..};

match expression: It's a bit like a switch, but a lot more flexible than switch, the wording is: match variable {0 => ...., 1 ... 3 => ....., 5 | 7 => ... .., _ => .....}, where the variable matching of variable, if the value is 0, a 1-3, 5 or 7, others, it is to be noted that each branch return It must be the same type;

Nine: Type

Numerical types: u8-u128 represents a byte unsigned value to a 16-byte unsigned value, corresponding to there i8-i128 indicates the sign; represents usize wherein there is an unsigned integer associated with the machine word; isize is signed;

Float is there f32 and f64;

Character type: char, it is 4 bytes (UTF-8);

Array type: [1,2,3] This is an array of objects, it may be a type definition [I32; 3] indicates the type of the front, rear indicates the length, but also with [1; 3] represents a length of 3 I32 type (default I32) and each array element of the initial value of 1;

Range Type: with (1..5) that such an approach is noted that the left and right to open and close, i.e. 1, 2, and writing the different mathematical; if it is fully closed (= 1 .. 5);

Slice Type: looks like is a reference to an array or vector or string?

String Type: str, which is similar in Java String, a string constant, the length of the content can not be changed; and Rust String is in the variable-length string;

never type: its type is called, does not necessarily mean the type of assignment, and it feels a lot like the type of unit (unit type is no meaning, similar to the void), such as panic macro type of the return value is due to the generation!!! the panic it must not return a value, so this concept in the return type is never type!

Tuple type: tuples is heterogeneous type and can be deconstructed by let tuple: (i32, & str) = (3, "bb"); such an approach, deconstruction tuple is let (x, y) = tuple ;, then 3 and "bb" on the respectively assigned to the x and y;

Structure: into a named structure (most common), a tuple structure and the unit structure body;

Named structure written as struct Peple {name: & str, gender: u32}; tuple structure is written as struct Color (i32, i32, i32) ;, cell structure is no attribute structure such as struct Empty; which is equivalent in struct Empty {}

Enumeration: enumeration has three forms, no parameters are enumerated, and the enumeration class C enumeration type parameter; the first was written enum Number {Zero, One}, the second is written enum Color {Red = 0xff0000 }, the value of such a structure can be converted to the corresponding base type value;

The third special wording as enum IpAddr {V4 (u8, u8, u8, u8)}, V4 trait is actually a function of the type (not implemented Fn closures or the like, is fn (u8, u8, u8, U8)), is called by enumeration value Number :: Zero; enumeration value and there may be two ways, such as enum Option {Some (i32), None}, first Some (i32) is a third form , while the first form is None;

 

to be continued. .

Guess you like

Origin www.cnblogs.com/silentdoer/p/11897793.html