While working with function pointer/reference for my embedded Rust project, I found reference to different functions are generating the same address (&foo and &bar), while actual function addresses are certainly different.
This itself was not a blocker for my project, but I'm interested in knowing what is happening behind this. Can anyone enlighten me what Rust is doing here?
It would have made sense for me if reference to a function resulted to the same address of the function itself (C-ism), or each reference to a function resulted to different addresses that represents something additionally allocated. Having the same address between different references is puzzling me.
fn foo() { println!("foo"); }
fn bar() { println!("bar"); }
fn main() {
let val1 = 100;
let val2 = 200;
println!("&val1={:p}", &val1);
println!("&val2={:p}", &val2);
println!("foo={:p}, &foo={:p}", foo as fn(), &foo);
println!("bar={:p}, &bar={:p}", bar as fn(), &bar);
}
Output:
&val1=0x7ffec48b53a0
&val2=0x7ffec48b53a4
foo=0x5631d26e5a20, &foo=0x5631d2706d24
bar=0x5631d26e5a60, &bar=0x5631d2706d24
Errors:
Compiling playground v0.0.1 (/playground)
Finished dev [unoptimized + debuginfo] target(s) in 0.44s
Running `target/debug/playground`