To switch to unsafe Rust, use the unsafe keyword and then start a new block that holds the unsafe code. You can take five actions in unsafe Rust that you can’t in safe Rust, which we call unsafe superpowers. Those superpowers include the ability to:
Dereference a raw pointer
Call an unsafe function or method
Access or modify a mutable static variable
Implement an unsafe trait
Access fields of unions
It’s important to understand that unsafe doesn’t turn off the borrow checker or disable any other of Rust’s safety checks:
This demonstrates that another important safety check is not performed in unsafe code:
unsafe extern "C" fn hotplug_callback(
ctx: *mut libusb_context,
device: *mut libusb_device,
event: libusb_hotplug_event,
user_data: *mut ::std::os::raw::c_void,
) -> ::std::os::raw::c_int {
println!("Event occured");
let dev_handle: *mut *mut libusb_device_handle = std::ptr::null_mut();
match usb_sys::libusb_open(device, dev_handle) {
0 => {
eprintln!("SUCCESS libusb_open")
}
LIBUSB_ERROR_NO_MEM => {
eprintln!("LIBUSB_ERROR_NO_MEM")
}
LIBUSB_ERROR_ACCESS => {
eprintln!("LIBUSB_ERROR_ACCESS")
}
LIBUSB_ERROR_NO_DEVICE => {
eprintln!("LIBUSB_ERROR_NO_DEVICE")
}
//NO ENTRY FOR REST OF THE ERRORS, NON THE LESS, IT COMPILES
}
0
}
Compiling playground v0.0.1 (/playground)
error[E0004]: non-exhaustive patterns: `i32::MIN..=-1_i32` and `1_i32..=i32::MAX` not covered
--> src/main.rs:7:15
|
7 | match foo() {
| ^^^^^ patterns `i32::MIN..=-1_i32` and `1_i32..=i32::MAX` not covered
|
= note: the matched value is of type `i32`
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern, a match arm with multiple or-patterns as shown, or multiple match arms
|
8 ~ 0 => (),
9 ~ i32::MIN..=-1_i32 | 1_i32..=i32::MAX => todo!(),
|
For more information about this error, try `rustc --explain E0004`.
error: could not compile `playground` due to previous error
This has nothing to do with unsafe or extern "C". To fix it, ensure that LIBUSB_ERROR_NO_MEM and similar are defined as consts and imported under the names you expect.