-
Notifications
You must be signed in to change notification settings - Fork 551
Description
Call expressions - The Rust Reference says:
For non-function types, the expression f(...) uses the method on one of the following traits based on the function operand:
- Fn or AsyncFn — shared reference.
- FnMut or AsyncFnMut — mutable reference.
- FnOnce or AsyncFnOnce — value.
The non-function types, as indicated by the link, refer to function item types.
Consider this example:
unsafe fn foo(i:i32){}
fn main(){
let f = foo as unsafe fn(i32);
unsafe{
f(0);
}
}
f is of type unsafe function pointer, fn - Rust says:
In addition, all safe function pointers implement Fn, FnMut, and FnOnce, because these traits are specially known to the compiler.
The wording emphasizes safe; in other words, unsafe function pointers don't implement Fn, FnMut, and FnOnce. Furthermore, the (unsafe)function pointers aren't function item types, so why is f(0)
a valid call expression?
More details can be seen https://users.rust-lang.org/t/why-can-the-unsafe-function-pointer-be-as-the-function-operand/133182