Why one static var has two addr?

here is my log:


and here is my code:

in mod global:

pub static mut TEST:TestStruct=TestStruct{ id: 1 };

pub struct TestStruct{
    pub id:i32

in mod magic:

fn test2(){
	unsafe {
		info!("test>>>>> {:p} | {:?}",&TEST,TEST);

in mod store:

fn test1(){
	unsafe {
		let a=&mut TEST;
		*a=TestStruct{ id: 22 };
	unsafe {
		info!("ptr》》》》》》: {:p} | {:?}",&TEST,TEST);

Please post a complete example. There are mistakes you could make in code you didn't show.


This looks to be something in code you haven't shared. I've turned what you shared into a Rust Playground, and I see:

   Compiling playground v0.0.1 (/playground)
    Finished dev [unoptimized + debuginfo] target(s) in 1.23s
     Running `target/debug/playground`
[2023-11-24T11:27:46Z INFO  playground::store] ptr》》》》》》: 0x563af7c66008 | TestStruct { id: 22 }
[2023-11-24T11:27:46Z INFO  playground::magic] test>>>>> 0x563af7c66008 | TestStruct { id: 22 }

It'd be helpful if you could share a complete reproducer of the problem.


From the reference:

Static items

StaticItem :
static mut ? IDENTIFIER : Type ( = Expression )? ;

A static item is similar to a constant, except that it represents a precise memory location in the program. All references to the static refer to the same memory location.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.