Rust basic syntax
Variables, basic types, functions, comments, and control flow are programming concepts that almost every programming language has.
These basic concepts will exist in every Rust program, and learning them early will enable you to learn the use of Rust as quickly as possible.
Variable
First of all, it must be stated that Rust is a strongly typed language, but it has the ability to automatically determine the type of variables. This can easily be confused with weakly typed languages.
If you want to declare a variable, you need to use the let
keyword. Forexample:
let a = 123;
Developers who have only studied JavaScript are very sensitive to this sentence, while developers who have only studied the C language do not understand it.
After this declaration statement, the following three lines of code are prohibited:
a = "abc";
a = 4.56;
a = 456;
The error in the first line is that when an is declared to be 123, an is determined to be an integer number and cannot be assigned a value of type string.
The error in the second line is that there is a loss of digital accuracy in automatic conversion, and the Rust language does not allow automatic data type conversion with loss of precision.
The error in the third line is that an is not a variable.
The first two mistakes are easy to understand, but what does the third one mean? Isn’t an a variable?
This involves the design of the Rust language for high concurrency security:at the language level, the values of variables can be changed as little as possible. So the value of an is immutable. But this does not mean that an isnot a “variable” (variable in English). Official documents refer to an asan “immutable variable”.
If part of the program we write runs under the assumption that the value will never change, and another part of our code is changing that value, thenthe first part of the code may not work as designed. Mistakes caused by this reason are difficult to find in hindsight. This is why the Rust language designed this mechanism.
Of course, it takes only one to mutable a variable mut
keyword.
let mut a = 123;
a = 456;
This program is correct.
The difference between constant and immutable variable
Since immutable variables are immutable, isn’t that a constant? Why is it called a variable?
There is a difference between variables and constants. In Rust, the following procedures are legal:
let a = 123; // Can be compiled, but there may be warnings as the variable is not being used
let a = 456;
But it is illegal if an is a constant:
const a: i32 = 123;
let a = 456;
The value of a variable can be “rebounded”, but it cannot be changed privately until it is “rebounded”, which ensures that the compiler can fully deduce program logic in the area after each “binding”. Although Rusthas the ability to determine types automatically, it is more convenient to declare types in some cases:
let a: u64 = 123;
It is declared here that an is an unsigned 64-bit integer variable. If the type is not 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 “re-binding” just described, and the reason for adding quotation marks is to replace the concept when it is not introduced.
Ghosting is a mechanism by which the name of a variable 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 running result of this program:
The value of x is: 12
The assignment of ghosting and variable variables is not a concept. Ghostingrefers to re-representing another variable entity with the same name, whosetypes, mutable attributes, and values can all be changed. However, variablevariable assignment can only change the value.
let mut s = "123";
s = s.len();
This program can go wrong: you cannot assign an integer value to a string variable.