Is it possible to declare 2 static mutable variables depending on each other?












3














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;
}









share|improve this question




















  • 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 '18 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 '18 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 '18 at 7:55
















3














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;
}









share|improve this question




















  • 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 '18 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 '18 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 '18 at 7:55














3












3








3







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;
}









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 15:18









Shepmaster

148k12283417




148k12283417










asked Nov 19 '18 at 14:43









Victor Polevoy

6,88133688




6,88133688








  • 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 '18 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 '18 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 '18 at 7:55














  • 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 at 7:55












1 Answer
1






active

oldest

votes


















8














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);
}
}





share|improve this answer























  • Holys... I did not know it was possible to put unsafe so... Thanks.
    – Victor Polevoy
    Nov 20 '18 at 7:53











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









8














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);
}
}





share|improve this answer























  • Holys... I did not know it was possible to put unsafe so... Thanks.
    – Victor Polevoy
    Nov 20 '18 at 7:53
















8














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);
}
}





share|improve this answer























  • Holys... I did not know it was possible to put unsafe so... Thanks.
    – Victor Polevoy
    Nov 20 '18 at 7:53














8












8








8






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);
}
}





share|improve this answer














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);
}
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 19 '18 at 15:39

























answered Nov 19 '18 at 14:49









hellow

4,78532042




4,78532042












  • Holys... I did not know it was possible to put unsafe so... Thanks.
    – Victor Polevoy
    Nov 20 '18 at 7:53


















  • Holys... I did not know it was possible to put unsafe so... Thanks.
    – Victor Polevoy
    Nov 20 '18 at 7:53
















Holys... I did not know it was possible to put unsafe so... Thanks.
– Victor Polevoy
Nov 20 '18 at 7:53




Holys... I did not know it was possible to put unsafe so... Thanks.
– Victor Polevoy
Nov 20 '18 at 7:53


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

SQL update select statement

'app-layout' is not a known element: how to share Component with different Modules