Is it possible to declare 2 static mutable variables depending on each other?
up vote
3
down vote
favorite
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;
}
rust
add a comment |
up vote
3
down vote
favorite
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;
}
rust
1
Not sure about your usecase, but this is a bad idea to do so, unless you really know what you do.
– Boiethios
Nov 19 at 14:51
1
Global, static, mutable variables are pure evil! Uselazy_static!
, RefCell or a Mutex, if you really have to, but I would avoid them at all cost!
– hellow
Nov 19 at 14:57
@hellow Oh, guys, of course I know that, I was just curious whether I can do so or not, because, you know, when I saw that I could not do this even withunsafe
block, 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.
– Victor Polevoy
Nov 20 at 7:55
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
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;
}
rust
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;
}
rust
rust
edited Nov 19 at 15:18
Shepmaster
145k11279413
145k11279413
asked Nov 19 at 14:43
Victor Polevoy
6,85633688
6,85633688
1
Not sure about your usecase, but this is a bad idea to do so, unless you really know what you do.
– Boiethios
Nov 19 at 14:51
1
Global, static, mutable variables are pure evil! Uselazy_static!
, RefCell or a Mutex, if you really have to, but I would avoid them at all cost!
– hellow
Nov 19 at 14:57
@hellow Oh, guys, of course I know that, I was just curious whether I can do so or not, because, you know, when I saw that I could not do this even withunsafe
block, 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.
– Victor Polevoy
Nov 20 at 7:55
add a comment |
1
Not sure about your usecase, but this is a bad idea to do so, unless you really know what you do.
– Boiethios
Nov 19 at 14:51
1
Global, static, mutable variables are pure evil! Uselazy_static!
, RefCell or a Mutex, if you really have to, but I would avoid them at all cost!
– hellow
Nov 19 at 14:57
@hellow Oh, guys, of course I know that, I was just curious whether I can do so or not, because, you know, when I saw that I could not do this even withunsafe
block, 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.
– Victor Polevoy
Nov 20 at 7:55
1
1
Not sure about your usecase, but this is a bad idea to do so, unless you really know what you do.
– Boiethios
Nov 19 at 14:51
Not sure about your usecase, but this is a bad idea to do so, unless you really know what you do.
– Boiethios
Nov 19 at 14:51
1
1
Global, static, mutable variables are pure evil! Use
lazy_static!
, RefCell or a Mutex, if you really have to, but I would avoid them at all cost!– hellow
Nov 19 at 14:57
Global, static, mutable variables are pure evil! Use
lazy_static!
, RefCell or a Mutex, if you really have to, but I would avoid them at all cost!– hellow
Nov 19 at 14:57
@hellow Oh, guys, of course I know that, I was just curious whether I can do so or not, because, you know, when I saw that I could not do this even with
unsafe
block, 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.– Victor Polevoy
Nov 20 at 7:55
@hellow Oh, guys, of course I know that, I was just curious whether I can do so or not, because, you know, when I saw that I could not do this even with
unsafe
block, 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.– Victor Polevoy
Nov 20 at 7:55
add a comment |
1 Answer
1
active
oldest
votes
up vote
8
down vote
accepted
Yes it is.
In your case, just remove mut
, because static globals are safe to access, because they cannot be changed and therefore do not suffer from all the bad attributes, like unsynchronized access.
static I: i64 = 5;
static J: i64 = I + 3;
fn main() {
println!("I: {}, J: {}", I, J);
}
If you want them to be mutable, you can use unsafe
where you access the unsafe variable (in this case I
).
static mut I: i64 = 5;
static mut J: i64 = unsafe { I } + 3;
fn main() {
unsafe {
println!("I: {}, J: {}", I, J);
}
}
Holys... I did not know it was possible to putunsafe
so... Thanks.
– Victor Polevoy
Nov 20 at 7:53
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
8
down vote
accepted
Yes it is.
In your case, just remove mut
, because static globals are safe to access, because they cannot be changed and therefore do not suffer from all the bad attributes, like unsynchronized access.
static I: i64 = 5;
static J: i64 = I + 3;
fn main() {
println!("I: {}, J: {}", I, J);
}
If you want them to be mutable, you can use unsafe
where you access the unsafe variable (in this case I
).
static mut I: i64 = 5;
static mut J: i64 = unsafe { I } + 3;
fn main() {
unsafe {
println!("I: {}, J: {}", I, J);
}
}
Holys... I did not know it was possible to putunsafe
so... Thanks.
– Victor Polevoy
Nov 20 at 7:53
add a comment |
up vote
8
down vote
accepted
Yes it is.
In your case, just remove mut
, because static globals are safe to access, because they cannot be changed and therefore do not suffer from all the bad attributes, like unsynchronized access.
static I: i64 = 5;
static J: i64 = I + 3;
fn main() {
println!("I: {}, J: {}", I, J);
}
If you want them to be mutable, you can use unsafe
where you access the unsafe variable (in this case I
).
static mut I: i64 = 5;
static mut J: i64 = unsafe { I } + 3;
fn main() {
unsafe {
println!("I: {}, J: {}", I, J);
}
}
Holys... I did not know it was possible to putunsafe
so... Thanks.
– Victor Polevoy
Nov 20 at 7:53
add a comment |
up vote
8
down vote
accepted
up vote
8
down vote
accepted
Yes it is.
In your case, just remove mut
, because static globals are safe to access, because they cannot be changed and therefore do not suffer from all the bad attributes, like unsynchronized access.
static I: i64 = 5;
static J: i64 = I + 3;
fn main() {
println!("I: {}, J: {}", I, J);
}
If you want them to be mutable, you can use unsafe
where you access the unsafe variable (in this case I
).
static mut I: i64 = 5;
static mut J: i64 = unsafe { I } + 3;
fn main() {
unsafe {
println!("I: {}, J: {}", I, J);
}
}
Yes it is.
In your case, just remove mut
, because static globals are safe to access, because they cannot be changed and therefore do not suffer from all the bad attributes, like unsynchronized access.
static I: i64 = 5;
static J: i64 = I + 3;
fn main() {
println!("I: {}, J: {}", I, J);
}
If you want them to be mutable, you can use unsafe
where you access the unsafe variable (in this case I
).
static mut I: i64 = 5;
static mut J: i64 = unsafe { I } + 3;
fn main() {
unsafe {
println!("I: {}, J: {}", I, J);
}
}
edited Nov 19 at 15:39
answered Nov 19 at 14:49
hellow
4,42432042
4,42432042
Holys... I did not know it was possible to putunsafe
so... Thanks.
– Victor Polevoy
Nov 20 at 7:53
add a comment |
Holys... I did not know it was possible to putunsafe
so... Thanks.
– Victor Polevoy
Nov 20 at 7:53
Holys... I did not know it was possible to put
unsafe
so... Thanks.– Victor Polevoy
Nov 20 at 7:53
Holys... I did not know it was possible to put
unsafe
so... Thanks.– Victor Polevoy
Nov 20 at 7:53
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53377010%2fis-it-possible-to-declare-2-static-mutable-variables-depending-on-each-other%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Not sure about your usecase, but this is a bad idea to do so, unless you really know what you do.
– Boiethios
Nov 19 at 14:51
1
Global, static, mutable variables are pure evil! Use
lazy_static!
, RefCell or a Mutex, if you really have to, but I would avoid them at all cost!– hellow
Nov 19 at 14:57
@hellow Oh, guys, of course I know that, I was just curious whether I can do so or not, because, you know, when I saw that I could not do this even with
unsafe
block, 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.– Victor Polevoy
Nov 20 at 7:55