I am trying to declare two static mutable variables but I have a error:
static mut I: i64 = 5;
static mut J: i64 = I + 3;
fn main() {
unsafe {
println!("I: {}, J: {}", I, J);
}
}
Error:
error[E0133]: use of mutable static is unsafe and requires unsafe function or block
--> src/main.rs:2:21
|
2 | static mut J: i64 = I + 3;
| ^ use of mutable static
|
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
Is it impossible? I also tried to put an unsafe block on the declaration but it seems to be incorrect grammar:
static mut I: i64 = 5;
unsafe {
static mut J: i64 = I + 3;
}
lazy_static!, RefCell or a Mutex, if you really have to, but I would avoid them at all cost!unsafeblock, I was stuck. I haven't had a real use case for this by now, but I wanted to know whether it was possible in general or not.