I'm working with a Rust cdylib
crate that I'm referencing and using in C++.
#[no_mangle]
pub extern "C" fn some_function(name: *const c_char, text: *const c_char) {
unsafe {
let name = CStr::from_ptr(name).to_str().unwrap();
let text = CStr::from_ptr(text).to_str().unwrap();
// the rest
}
}
When this function receives the character ±, it panics when attempting to get the text from the pointer. I'm passing this character in as a c_str()
in C++ from a std::string
:
thread '<unnamed>' panicked at 'called `Result::unwrap()` on an `Err` value: Utf8Error { valid_up_to: 0, error_len: Some(1) }', src\lib.rs:102:50
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Is there any way that I can properly handle this character in Rust? I don't need to manipulate it in any way, realistically this library is simple acting as a middle man, and just needs to pass it along.
When I use this to view the bytes I'm receiving:
let raw = CStr::from_ptr(text);
println!("Bytes: {:?}", raw.to_bytes_with_nul());
I get:
Bytes: [177, 0]