'Why does Rust return a different address for a vector and its first element?

I am trying to print the address of a vector.

fn main() {
    let g = vec![1,2,3];
    println!("great {:p}, {:p}", &g, &g[0])
}

This prints great 0x7fff379f9e90, 0x564dc99e29d0

I am wondering why the addresses are different. To the best of my knowledge, I can't directly print g as we could do in C++. So I did &g. That should still give the address of the vector (the starting point) which is the same thing as &g[0].

Why are they different?



Solution 1:[1]

According to the documentation, when you print &g you are printing the address in the stack of the structure definition of Vec

Documentation:

https://doc.rust-lang.org/std/vec/struct.Vec.html

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 EliaTolin