0

I am building a struct called DATAMAP which is designed to hold a vector of TCPStreams.

After I declare the struct, I proceed to instantiate a global const reference to it.

The issue I am having is that the global const pointer address seems to be changing from one method call to the next. Note there are no assignments, just referenced usage. I am simply trying to use the struct and access my Vec called 'connections'. Below are the declarations:

//DECLARATIONS

struct datamap {
    connections: Vec<TcpStream>,
}

impl datamap {
    fn push(&mut self, conn: TcpStream) {
        println!("Adding connection to vec");
        self.connections.push(conn);
    }
    fn len(self) -> usize {
        return self.connections.len();
    }
}

impl fmt::Pointer for datamap {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:p}", self as *const datamap)
    }
}

//  CREATE A STRUCT - Note we are global scope here
const app: datamap = datamap {
    connections: Vec::new(),
};

//  MAIN
fn main() {
    println!("The address of the app struct is {:p}", app);
    some_other_method(); // call another scope method to see issue
}

fn some_other_method() {
    println!("The address of the app struct is {:p}", app);
}

The output will end up like:

The address of the app struct is 0x7ffee8613180
The address of the app struct is 0x7ffee8612fb8

How is the const getting a new address?

mcarton
  • 27,633
  • 5
  • 85
  • 95
Neil
  • 413
  • 1
  • 3
  • 11
  • By the way, idiomatic Rust uses `snake_case` for variables, methods, macros, fields and modules; `UpperCamelCase` for types and enum variants; and `SCREAMING_SNAKE_CASE` for statics and constants. Please adhere to these idioms. – Shepmaster Jun 26 '19 at 16:07
  • It's hard to answer your question because it doesn't include a [MRE]. We can't tell what imports are present in the code, and attempting to guess produces a compiler error, not the output you are asking about. It would make it easier for us to help you if you try to reproduce your error on the [Rust Playground](https://play.rust-lang.org) if possible, otherwise in a brand new Cargo project, then [edit] your question to include the additional info. There are [Rust-specific MRE tips](//stackoverflow.com/tags/rust/info) you can use to reduce your original code for posting here. Thanks! – Shepmaster Jun 26 '19 at 16:10
  • See also [How do I create a global, mutable singleton?](https://stackoverflow.com/q/27791532/155423). Besides not working, your code would be horribly broken once threads come into play. – Shepmaster Jun 26 '19 at 16:11
  • Nope, why do you ask? – Shepmaster Jun 26 '19 at 16:22
  • Well today I am learning about the nuances of global structs and memory references in rust, it seems you need working code and unicorns to fill up a multi-threaded app that doesn't exist. – Neil Jun 26 '19 at 16:27
  • I would appreciate a more technical answer on why the const usage creates the new allocation. What is happening at the stack / heap level to make this so in the different scopes. – Neil Jun 26 '19 at 16:32
  • 1
    @Neil that nothing link to the "stack" or the "heap", the duplicate already answer the technical part. const in Rust are not what you think use static. – Stargateur Jun 26 '19 at 16:36
  • Agreed, I had static working earlier but it led wrapping usage blocks as unsafe due to the "mutable static" safety issue. I'll look at passing mutable references and build it in the main. – Neil Jun 26 '19 at 16:48

0 Answers0