Rust basic syntax

Rust basic syntax

Hello World

fn main() {
    println!("Hello, world!");
}

Rust data types

Rust is a statically typed language. Every value in Rust is some kind of data type. The compiler can automatically infer the data type of a variable based on the value assigned to it.

declare variables

Declare variables using the let keyword.

In the case of direct assignment, Rust will automatically infer the variable type, which can be easily viewed in the IDE.

Image1

fn main() {
   let company_string = "TutorialsPoint";  // string 类型
   let rating_float = 4.5;                 // float 类型
   let is_growing_boolean = true;          // boolean 类型
   let icon_char = '♥';                    //unicode character 类型

   println!("company name is:{}",company_string);
   println!("company rating on 5 is:{}",rating_float);
   println!("company is growing :{}",is_growing_boolean);
   println!("company icon is:{}",icon_char);
}
在上面的代码示例中,变量的数据类型是从被分配的值而推断出来的。例如,变量 company_string 被 Rust 分配了 string 字符串数据类型,变量 rating_float 被分配了 float 浮点数据类型,等等。

The println! macro function has two parameters

One parameter is a placeholder { }, special syntax
One parameter is a variable or constant
The placeholder will be replaced by the value of the variable drop

The above code snippet will output the following

company name is: TutorialsPoint
company rating on 5 is:4.5
company is growing: true
company icon is: ♥

It should be noted that Rust variables are immutable variables by default.

Rust is designed for cake development. When its variables are declared using the let keyword, they are not edgeable by default.

for example:

let a = 123;

And the next three sentences

a = "abc";
a = 4.56; 
a = 456;

An error will be reported, so why?

The error in the first line is that after declaring a to be 123, a is determined to be an integer number, and a string type value cannot be assigned to it.

The error in the second line is that automatic conversion of numbers causes loss of precision. The Rust language does not allow automatic data type conversion with loss of precision.

The error in line 3 is that a is not a mutable variable.

The first two errors are easy to understand, but what does the third one mean? Isn't a a variable?

This involves the design of the Rust language for high concurrency safety: allowing the values ​​of variables to be changed as little as possible at the language level. So the value of a is immutable. But this does not mean that a is not a "variable" (variable in English). The official documentation calls a variable like a an "immutable variable".

If we write one part of the program that operates under the assumption that a value never changes, and another part of our code changes that value, then the first part of the code may not behave as designed. Errors due to this cause are difficult to find after the fact. This is why the Rust language was designed with this mechanism in mind.

Of course, making a variable "mutable" only requires a mut keyword.

let mut a = 123;
a = 456;

This way the program can run

The difference between constants and immutable variables

Since immutable variables are immutable, aren't they constants? Why are they called variables?

There is a difference between variables and constants. In Rust, the following programs are legal:

let a = 123; // Compiles, but may have warnings because the variable is not used
let a = 456;

But it's not legal if a is a constant:

const a: i32 = 123;
let a = 456;

The value of a variable can be "rebound", but it cannot be changed privately before "rebinding". This ensures that the compiler can fully reason about the program in the area after each "binding" logic. Although Rust has the function of automatically determining types, in some cases it is more convenient to declare the type:

let a: u64 = 123;

Here, a is declared as an unsigned 64-bit integer variable. If no type is declared, a will automatically be judged as a signed 32-bit integer variable, which has a great impact on the value range of a.

Shadowing

The concept of ghosting is different from "Override" or "Overload" in other object-oriented languages. Ghosting is the so-called "rebinding" just described. The reason why the quotation marks are added is to replace the concept when this concept is not introduced.

Ghosting is a mechanism whereby variable names can be reused:

Example

fn main() {  
    let x = 5;  
    let x = x + 1;  
    let x = x * 2;  
    println!("The value of x is: {}", x);  
}  

The result of running this program:

The value of x is: 12

Ghosting and assignment of variable variables are not the same concept. Ghosting refers to using the same name to re-represent another variable entity, and its type, variable attributes and values ​​can all change. But mutable variable assignment can only change the value.

let mut s = "123";
s = s.len();

This program will give an error: an integer value cannot be assigned to a string variable.

おすすめ

転載: blog.csdn.net/qq_42901723/article/details/135031016