Pattern match a function pointer in default arm

I have a function pointer as part of an enum. I can put it in, but I can't seem to figure out how to extract it back out.

Basically, I'd like to know what to put as ZZZ to make Rust quit complaining. I figure something null() with a cast, but this is all no_std so I'm not sure where that would be.

Alternatively, if there is a better way to do what I want, I'm open to that too.


enum EN {
    PTR{fp: fn (ll: i32, rr:i32) -> i32},

fn foo(ll: i32, rr: i32) -> i32 {

fn matchme(qq: EN) {
    let bb: fn (ll: i32, rr:i32) -> i32 = match qq {
        EN::PTR{fp: matched_fp} => matched_fp,
        _ => ZZZ,
    println!("fp: {:?}", bb);

fn main() {
    let aa = EN::PTR{fp: foo};



   Compiling playground v0.0.1 (/playground)
error[E0425]: cannot find value `ZZZ` in this scope
  --> src/
15 |         _ => ZZZ,
   |              ^^^ not found in this scope

For more information about this error, try `rustc --explain E0425`.
error: could not compile `playground` due to previous error

It's all about what do you want when you don't have that function pointer. If you somehow know this case never happen so don't want to care it further, you can just _ => unreachable!(), it - it panic if it actually happens.

This is UB. In Rust function pointers can't be null, same as references. That's why the bindgen translates function pointer types in C to Option<fn()>-like types.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.