I wanted to create a function pointer using the reference operator:
fn main() {
let fcn_ptr = &add;
println!("{:p}", fcn_ptr);
println!("{}", fcn_ptr(10, 10));
}
fn add(x: i32, y: i32) -> i32 {
x + y
}
The compiler output:
warning: taking a reference to a function item does not give a function pointer
--> src/main.rs:4:22
|
4 | println!("{:p}", fcn_ptr);
| ^^^^^^^ help: cast `add` to obtain a function pointer: `add as fn(_, _) -> _`
|
= note: `#[warn(function_item_references)]` on by default
And the execution output:
0x55c0890fa000
20
What are the reasons behind the warning?