English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Variables, basic types, functions, comments, and control flow, these are almost programming concepts that 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 at the fastest speed.
Firstly, it must be noted that Rust is a strongly typed language, but it has the ability to automatically judge the type of variables. This is easy to confuse with weakly typed languages.
To declare a variable, you need to use the let keyword. For example:
let a = 123;
Developers who have only learned JavaScript are very sensitive to this sentence, while developers who have only learned C language do not understand this sentence at all.
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 declaring a as 123 From now on, a is determined to be an integer type and cannot be assigned a value of the string type.
The error in the second line is that there is a loss of precision in the automatic conversion of numbers, Rust language does not allow automatic data type conversion with precision loss.
The error in the third line is that a is not a mutable variable.
The first two types of errors are easy to understand, but what does the third one mean? Isn't a a variable?
This involves Rust's design for high concurrency safety: on the language level, as few variable values as possible can be changed. Therefore, the value of a is immutable. However, this does not mean that a is not a 'variable' (in English, variable), the official documentation calls such variables 'immutable variables'.
If a part of the program we write runs under the assumption that a value will never change, while another part of our code changes that value, the first part of the code may not operate as intended. Errors caused by this reason are difficult to find after the fact. This is why Rust language designs this mechanism.
Of course, making a variable 'mutable' only requires a mut keyword.
let mut a = 123; a = 456;
This program is correct.
Since immutable variables are immutable, aren't they constants? Why are they called variables?
There is still a difference between variables and constants. In Rust, the following program is legal:
let a = 123; let a = 456;
But it is not legal if a is a constant:
const a: i32 = 123; let a = 456;
The value of a variable can be "re-bound", but it cannot be changed privately before re-binding, which ensures that the compiler can fully infer the program logic after each "binding". Although Rust has the function of automatic type judgment, in some cases, declaring the type is more convenient:
let a: u64 = 123;
Here, a is declared as unsigned 64 Bit integer variables, if not declared with a type, will be automatically judged as signed. 32 Bit integer variables, which have a significant impact on the value range of a.
The concept of shadowing is different from "override" or "overload" in other object-oriented languages. Shadowing is what we call "re-binding", and the quotation marks are used to represent this concept when it has not been introduced yet.
Shadowing refers to the mechanism that allows variable names to be reused:
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 is:
The value of x is: 12
Shadowing and assignment of mutable variables are not the same concept. Shadowing refers to the mechanism of reusing the same name to represent another variable entity, which can change in type, mutability, and value. However, assignment of mutable variables can only change the value.
let mut s = "123"; s = s.len();
This program will fail: You cannot assign an integer value to a string variable.