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 Rust usage at the fastest speed.
### Variables
First, it must be stated that Rust is a strongly typed language, but it has the ability to automatically infer variable types. This can easily cause confusion with weakly typed languages.
By default, variables in Rust are immutable, unless declared as mutable variables using the mut keyword.
let a = 123; // Immutable variable
let mut b = 10; // Mutable variable
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 statement, while developers who have only learned C language don't understand it very well.
After this declaration, the following three lines of code are all prohibited:
a = "abc";
a = 4.56;
a = 456;
The error in the first line is that after declaring a as 123, a is determined to be an integer, and you cannot assign a string value to it.
The error in the second line is that automatic type conversion causes precision loss, and Rust 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: to minimize the ability to change variable values at the language level. So the value of a is immutable. But this doesn't mean a is not a "variable" (variable in English). The official documentation refers to a as an "immutable variable".
If one part of our program runs assuming a value will never change, while another part of our code is changing that value, then the first part of the code may not work as intended. Errors caused by this kind of reason are very difficult to find afterward. This is the reason Rust designed this mechanism.
Of course, making a variable "mutable" only requires the mut keyword.
let mut a = 123;
a = 456;
This program is correct.
### Difference Between Constants and Immutable Variables
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; // Can compile, but may have a warning because the variable is not used
let a = 456;
But if a is a constant, it's illegal:
const a: i32 = 123;
let a = 456;
The value of a variable can be "rebound", but it cannot be privately changed before "rebinding". This ensures that after each "binding", the compiler can fully reason about the program logic in that region. Although Rust has automatic type inference, declaring the type is more convenient in some cases:
let a: u64 = 123;
Here a is declared as an unsigned 64-bit integer variable. If the type is not declared, a will automatically be inferred as a signed 32-bit integer variable, which has a great impact on the range of values for a.
### Data Types
Rust is a statically typed language. You can explicitly specify types when declaring variables, but you can usually rely on type inference.
**Basic Types:**
i32 (32-bit signed integer), u32 (32-bit unsigned integer), f64 (64-bit floating point), bool (boolean), char (character)
## Example
let x:i32=42;
let y:f64=3.14;
let is_true:bool=true;
let letter:char='A';
### Functions
Rust functions are defined using the fn keyword, and the return type of a function is specified using the arrow symbol ->.
## Example
fn add(a:i32, b:i32)->i32{
a + b
}
If a function has no return value, the type defaults to () (i.e.,
YouTip