-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathstatics.alu
38 lines (30 loc) · 1.11 KB
/
statics.alu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
use std::io::{print, println};
// Static variables are initialized in dependency order and if there
// are cycles in the graph, a compile-time error will be produced.
// If a variable is unused, initialization code is not guaranteed to run.
static global_var1: i32 = hello();
static global_var2: i32 = global_var3 + 1;
static global_var3: i32 = global_var4 + 1;
static global_var4: i32 = global_var1 + 1;
static global_var5: i32 = global_var6 + 1;
static global_var6: i32 = if global_var2 > 2 { global_var2 } else { 0 };
static global_var7: i32 = loop {
if global_var5 > 1000 {
break global_var5
}
global_var5 *= 2;
};
// Statics can also be generic. This can be useful to create associated statics for
// multiple distinct types
static TYPE_NAME<T>: &[u8] = std::typing::type_name::<T>();
fn hello() -> i32 {
3
}
fn main() {
println!("{}", global_var7);
/// Each instance of a generic static is distinct, but also mutable
TYPE_NAME::<i32> = "32-bit signed integer";
println!("{}", TYPE_NAME::<i32>);
println!("{}", TYPE_NAME::<i64>);
println!("{}", TYPE_NAME::<Option<i32>>);
}