RUST Learning Diary Lesson 4 – The RUST specification


0 x00 review

In the last section we looked at the common ides for Rust, and choosing the right one can help you write more code with less effort. In this section let’s take a look at some of the common conventions and conventions of Rust. Learning a programming language is like learning a foreign language. When in Rome, do as the Romans do and respect the habits of the language.

0x01 Program entry point

We’re now in section 4, and as you’ve probably noticed, every time you create a project you generate a default function called main, which in Java is called a method. The main function is the entry point to the program, which is necessary for all binary executables. Without it, the program would have no entry point and would not be able to execute.

fn main(){

}
Copy the code

Just a quick look at how it works. Declare a function that starts with fn, which is short for function, followed by the function name main, followed by parameters, and since main has no parameters, you don’t need to declare parameters, followed by curly braces, and you can write code in curly braces.

PS: The functions will be explained in detail in the following chapters. You can read them here.

Parameters and return values of the 0x02 main function

The main method in Rust has no arguments and no return value. Is it a little subversive of your “three views”.

fn main() {
    for arg in std::env::args()
    {
        println!("{}", arg);
    }

    // Returns a value, 0 by default
    std::process::exit(0);
}
Copy the code

In fact, Rust has a special function to handle incoming arguments — STD ::env::args(), which can accept all arguments. You can click into the source code to see the notes, notes written in detail. For the return value of the function, use STD ::process::exit(0); Return, where the input parameter is the return value.

0x03 Naming conventions

1. Snake Case

File name: for example, hello_world. Rs, main_XXX.rs

Variable name: For example, zhangsan_name

Function name: for example: func_name()

2. Camel Case

Struct ExampleStruct {name: String}

Enum Type: For example: enum IpAddress {IPV4(U8, U8, U8, U8)}

3, other

Associated constants: all uppercase, for example, NAME and AGE

Concatenation: Cargo converts the concatenation “-” to an underscore “_” by default

Statements: As in C, Java, etc., add at the end of each statement;

PS: It is not recommended that Rust name packages with the suffix ** “-rs” or “_rs**”. If you name packages with this suffix, the suffix will be removed.

0x04 Standard output

In the previous article, we learned how to use println! Go ahead and print the content. Now let’s go over it.

The four common output instructions in Rust are outlined in red. The following figure is from the official Rust documentation.

The difference between print and println is one more ln. Print is output (without newline), println is output and newline. Similarly, eprint and eprintln are the same thing.

Q:printandeprintWhat’s the difference?

If you run it through the code you can’t see the difference. Look at the documentation then.

The document says that the output is stderr instead of stdout, and that should be clear. Those of you who have a background in other languages should be aware of this. A brief description of the differences between the two:

Stdout: standard output device. The default is line buffering, and its output data is stored in a buffer that is printed to the screen when a line breaks. If the program outputs to a file, it outputs to a file.

Stderr: Standard error output device. The default is unbuffered and outputs data directly. If the program moves to output to a file, it will still output to the screen.

Let’s use the file to do a test, on the code:

fn main() {
    println!("I'm a println!"); eprintln! ("I am eprintln!");
}
Copy the code

Blue box on the right: Two lines of data are printed, no difference can be seen.

The red box on the left: Output to the test. TXT file. Output the contents of eprintln in the terminal and println in the file.

PS: If you want to know more about the difference between println and println, you can search for println again in a later chapter.

0x05 dbg!

If you want to debug output, use DBG! This command encapsulates eprintln. Print content through it, output content with file name, line number and other information, can be very convenient program debugging. You are advised to use DBG! To print logs. Instead of a println! . The next section is about learning Rust.

PS: Use DBG! This is required after version 1.32.0 (included) of Rust.

Example code is as follows:

dbg! ("I'm a debug log.");
Copy the code

0 x06 data

Print document – Rust (rust-lang.org)

STD :: DBG documentation – Rust (rust-lang.org)

0x07 This section source code

004 · StudyRust – Code Cloud – Open Source China (gitee.com)

Next up — variables and constants.