Aravindh S
Aravindh S

Reputation: 1245

Why are the address two different values the same when printed as function parameters?

fn main() {
    println!("{:p}", &"aravindh");
    println!("{:p}", &"test");
    address_of(&"aravindh");
    address_of(&"test");
}

fn address_of<A>(a:&A){
    println!("{:p}", &a);
}

The output is:

0x10da08260
0x10da08278
0x7fff52231990
0x7fff52231990

While the address of first two strings are different, why are they same when printed via the address_of function?

Upvotes: 2

Views: 45

Answers (1)

DK.
DK.

Reputation: 58975

Because you're printing the address of the parameter a, not the address of the thing it points to.

fn address_of<A>(a: &A){
    println!("{:p}", a);
}

Upvotes: 4

Related Questions