Easy Tutorial
❮ Rust Println Rust Loop ❯

Rust Basic Syntax

Variables, basic types, functions, comments, and control flow are programming concepts that almost every programming language has.

These fundamental concepts will exist in every Rust program, and learning them early will allow you to learn Rust usage as quickly as possible.

Variables

First, it must be stated that Rust is a strongly typed language but has the ability to automatically infer variable types. This can easily cause confusion 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 statement, while those who have only learned C may not understand it.

After this declaration, the following three lines of code are prohibited:

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

The error in the first line is that once a is declared as 123, it is determined to be an integer, and you cannot assign a string value to it.

The error in the second line is due to the automatic conversion of numeric precision, which Rust does not allow if it results in a loss of precision.

The error in the third line 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 brings us to the design of Rust for high concurrency safety: at the language level, the value of variables is changed as little as possible. So the value of a is immutable. However, this does not mean that a is not a "variable" (in English, a variable). The official documentation refers to such variables as "immutable variables."

If a part of our program runs on the assumption that a value will never change, and 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 afterward. This is the reason for Rust's design of 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 just 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 is not legal:

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

The value of a variable can be "rebound," but it cannot be changed privately before "rebinding," ensuring that the compiler can fully infer the program logic in the area after each "binding." Although Rust has the ability to automatically infer types, sometimes declaring types is more convenient:

let a: u64 = 123;

Here, a is declared as an unsigned 64-bit integer variable. If the type is not declared, a will be automatically inferred as a signed 32-bit integer variable, which has a significant impact on the range of values for a.

Shadowing

The concept of shadowing is different from "overriding" or "overloading" in other object-oriented languages. Shadowing is what was previously described as "rebinding." The quotation marks were added to substitute for the concept before introducing it.

Shadowing refers to the mechanism where 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 result of running this program:

The value of x is: 12

Shadowing is not the same concept as mutable variable assignment. Shadowing means that the same name can represent another variable entity, and its type, mutability, and value can all change. But mutable variable assignment only allows changes in value.

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

This program will error: you cannot assign an integer value to a string variable.

❮ Rust Println Rust Loop ❯