“Expected fn item, found a different fn item” when working with function pointers

前端 未结 2 597
孤独总比滥情好
孤独总比滥情好 2020-12-01 14:20

I have the following code (Playground):

// Two dummy functions, both with the signature `fn(u32) -> bool`
fn foo(         


        
2条回答
  •  南方客
    南方客 (楼主)
    2020-12-01 15:21

    Each named function has a distinct type since Rust PR #19891 was merged. You can, however, cast the functions to the corresponding function pointer type with the as operator.

    call_both(foo as fn(u32) -> bool, bar as fn(u32) -> bool);
    

    It's also valid to cast only the first of the functions: the cast will be inferred on the second, because both functions must have the same type.

    call_both(foo as fn(u32) -> bool, bar);
    

提交回复
热议问题