In this paper, starting from my zhihu column: zhuanlan.zhihu.com/p/110107918 reprint please indicate the source

Rust constants (const)

  • The definition is as follows:

    const NUM:i32=100	
    Copy the code
  • Defining constants is different from defining ordinary variables

    • The keyword is ***const** instead of ***let**

    • You must specify the variable type (e.g. ***i32***) and cannot omit it.

      const NUM=100	//Compile Error!
      Copy the code
    • When defining constants, variables are usually named in all uppercase (such as ***NUM***). It is not mandatory, but warning is included.

    • Constants can be defined in any scope and defined throughout the life of the program. At compile time, the compiler inlines it as much as possible into the code, so references to the same constant in different places do not guarantee to refer to the same memory address.

    • Constants can only be assigned to constant/mathematical expressions, that is, values that can be computed at compile time. Values that need to be computed at runtime, such as functions, cannot be assigned to constant expressions.

    • Variable masking occurs when there is a duplicate definition (binding) of a variable, and later variables mask the previous ones. For constants, duplicate definitions are not allowed. For example, the following code will report an error:

      #[test]
      fn test_define_same_const_variable() {const NUM:i32=100;
          const NUM:f64=200.0;//error[E0428]: the name `NUM` is defined multiple times
      }
      
      Copy the code

Rust global (static)

  • Are defined as follows
static NUM: i32 = 100;
Copy the code
  • Global variables are similar to constants, but one important difference is that they are not inlined. There is only one instance of a global variable throughout the program, which means all references to the same address.
  • Define a global variable using the static keyword * * * * * *, and defining constants to use * * * * * * const, common variables used let * * * * * *
  • Unlike constants, global variables can be defined as *** variable (mut)*** as follows:
static mut NUM:i32 = 100
Copy the code

Even though the variable is mutable, it can be accessed by multiple threads at the same time, making it memory-unsafe. Therefore, the code to access and modify a static mut must be defined in the unsafe block, as in the following example:

unsafe {
    NUM += 1;
    println!("NUM: {}", NUM);
}
Copy the code
  • The value stored in the global (static) variable must be Sync, which means the Sync trait needs to be implemented.
  • Like constants, global variables must be assigned when they are defined, and the assignment must be a compile-time value (constant expression/mathematical expression), not a run-time value (such as a function)

How to choose whether to use const or global variables (static)

In general, if you have to choose between the two, select const ***. Using global variables always takes up a small space in memory, but constants can be optimized at compile time (inlined). Not only can you optimize your own Crate, but if someone else uses yours, You can also europeanize.

Welcome to my official account “Rust”.

Reference links:

www.twle.cn/c/yufei/rus…

Doc.rust-lang.org/1.0.0/book/…