将命名函数作为函数指针传递

时间:2016-08-22 00:58:34

标签: rust function-pointers

将命名函数作为函数指针传递的正确方法是什么?

static mut foo: *mut fn(buf: Vec<u8>) = 0 as *mut fn(buf: Vec<u8>);


fn main() {
    bar(&mut baz);
}

fn bar(f: &mut fn(buf: Vec<u8>)) {
    unsafe { foo = f };
}

fn baz(buf: Vec<u8>) {}

产地:

error: mismatched types [--explain E0308]
 --> <anon>:5:9
  |>
5 |>     bar(&mut baz);
  |>         ^^^^^^^^ expected fn pointer, found fn item
note: expected type '&mut fn(std::vec::Vec<u8>)'
note:    found type '&mut fn(std::vec::Vec<u8>) {baz}'

0 个答案:

没有答案