:timing
:sccache 1
:dep once_cell = "1.13.0"
use once_cell::sync::Lazy;
let¶
By default,
letdefines an immutble variable. To define a mutable variable, you have to uselet mut.letcan only be used in a function, which means that you cannot define a variable usingletin a module.constandstaticcan 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
constorstatickeyword.Below summarizes the difference between
letandconst.mutis NOT allowed withconstsince a const can never be changed.constrequires explit types whileletsupport type inference.A
constvariable is resolved and inlined at compile time which means that aconstvariable 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
staticandconst.Both
constandstaticcan be considered as defining constant values.constis the traditional compile-time concept as in C++ (different fromfinalin Java which is not a compile-time concept).staticdefines a static lifetime variable. A variable defined usingstaticis usually used as immutable (and thus can be treated as a constant). Accessing or modifiying a mutablestaticvariable can only done throughunsafecode.Both
constandstaticrequires 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
staticvariable 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¶