:timing
:sccache 1
:dep once_cell = "1.13.0"
use once_cell::sync::Lazy;
let¶
By default,
let
defines an immutble variable. To define a mutable variable, you have to uselet mut
.let
can only be used in a function, which means that you cannot define a variable usinglet
in a module.const
andstatic
can be used in any scope.
let x = 1;
x
x = 2
let mut¶
let mut y = 2;
y = 3;
y
const
and static
Variables¶
You must specify the type of a variable explicitly when defining it using the
const
orstatic
keyword.Below summarizes the difference between
let
andconst
.mut
is NOT allowed withconst
since a const can never be changed.const
requires explit types whilelet
support type inference.A
const
variable is resolved and inlined at compile time which means that aconst
variable must be a constant expression which can be determined at compile time. It cannot be the result of a function call or anything that could only be determined at runtime.
Below summarizes the differences between
static
andconst
.Both
const
andstatic
can be considered as defining constant values.const
is the traditional compile-time concept as in C++ (different fromfinal
in Java which is not a compile-time concept).static
defines a static lifetime variable. A variable defined usingstatic
is usually used as immutable (and thus can be treated as a constant). Accessing or modifiying a mutablestatic
variable can only done throughunsafe
code.Both
const
andstatic
requires explicit types.A const variable is inlined into the code at compile time, which means that it is not a good choice for defining large data. Defining large data as a
static
variable is a better alternative.Currently, you can associate a const variable with a struct but you cannot associate a static variable with a struct. However, this might change in future as there is a Pre-RFC: Associated statics proposing supporting associated statics.
You probably do not really need const ...
For more discussions, please refer to Const vs static .
const¶
You must specify a type when defining a const variable, o.w., the code won't compile.
const my_const_var = 1;
const my_const_var: i32 = 1;
static¶
When defining a static variable which implements Copy, you can define it as a static reference. This helps avoiding accidental implicity copies. A more idiomatic way is to define a wrapper non-Copy struct so that you can be sure that the underlying data won't get copied.
An example where a static array is copied implicitly.
static ARR: [usize; 10] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
let a = [ARR, ARR, ARR];
Define a static variable.
mod abc {
pub static x: i8 = 1;
}
abc::x
static GLOBAL_VEC: Lazy<Vec<i32>> = Lazy::new(|| {
let mut v = Vec::new();
v.push(1);
v.push(10);
v.push(20);
v
});
fn print_vec(v: &[i32]) {
v.iter().for_each(|i| {
println!("{i}");
});
}
print_vec(&GLOBAL_VEC)
static GLOBAL_ARR: Lazy<[i32; 3]> = Lazy::new(|| {
[100, 200, 300]
});
fn print_arr(v: &[i32]) {
v.iter().for_each(|i| {
println!("{i}");
});
}
let arr: &[i32; 3] = &GLOBAL_ARR;
print_arr(arr)
References¶