Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created April 20, 2019 03:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rust-play/b70c21240c9bc9fb5eadcc083852c276 to your computer and use it in GitHub Desktop.
Save rust-play/b70c21240c9bc9fb5eadcc083852c276 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
extern fn foo() {}
extern "C" {
// This doesn't cast `Some(foo)` to the correct type
fn printf(fmt: *const u8, ...);
// This works correctly
//fn printf(fmt: *const u8, _: Option<extern fn()>);
}
fn main() {
unsafe {
// ERROR: printf(b"foo:%p\n\x00".as_ptr(), foo);
printf(b"foo:%p\n\x00".as_ptr(), foo as extern fn());
printf(b"foo:%p\n\x00".as_ptr(), Some(foo));
printf(b"foo:%p\n\x00".as_ptr(), Some(foo as extern fn()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment