Function item types
When referred to, a function item, or the constructor of a tuple-like struct or enum variant, yields a zero-sized value of its function item type.
That type explicitly identifies the function - its name, its type arguments, and its early-bound lifetime arguments (but not its late-bound lifetime arguments, which are only assigned when the function is called) - so the value does not need to contain an actual function pointer, and no indirection is needed when the function is called.
There is no syntax that directly refers to a function item type, but the
compiler will display the type as something like fn(u32) -> i32 {fn_name}
in
error messages.
Because the function item type explicitly identifies the function, the item types of different functions - different items, or the same item with different generics - are distinct, and mixing them will create a type error:
However, there is a coercion from function items to function pointers with
the same signature, which is triggered not only when a function item is used
when a function pointer is directly expected, but also when different function
item types with the same signature meet in different arms of the same if
or
match
:
All function items implement Fn
, FnMut
, FnOnce
, Copy
,
Clone
, Send
, and Sync
.