Function std::ffi::c_str_to_bytesUnstable [-]  [+] [src]

pub unsafe fn c_str_to_bytes<'a>(raw: &'a *const c_char) -> &'a [u8]

Interpret a C string as a byte slice.

This function will calculate the length of the C string provided, and it will then return a corresponding slice for the contents of the C string not including the nul terminator.

This function will tie the lifetime of the returned slice to the lifetime of the pointer provided. This is done to help prevent the slice from escaping the lifetime of the pointer itself. If a longer lifetime is needed, then mem::copy_lifetime should be used.

This function is unsafe because there is no guarantee of the validity of the pointer raw or a guarantee that a nul terminator will be found.

Example

extern crate libc; fn main() { use std::ffi; use std::str; use libc; extern { fn my_string() -> *const libc::c_char; } unsafe { let to_print = my_string(); let slice = ffi::c_str_to_bytes(&to_print); println!("string returned: {}", str::from_utf8(slice).unwrap()); } }
use std::ffi;
use std::str;
use libc;

extern {
    fn my_string() -> *const libc::c_char;
}

unsafe {
    let to_print = my_string();
    let slice = ffi::c_str_to_bytes(&to_print);
    println!("string returned: {}", str::from_utf8(slice).unwrap());
}