mirror of
https://github.com/rust-lang/rust
synced 2024-11-02 13:50:48 +00:00
Add doc example for CString::from_raw
.
This commit is contained in:
parent
d3c26fe7e3
commit
93abc2f877
1 changed files with 21 additions and 0 deletions
|
@ -287,6 +287,27 @@ pub unsafe fn from_vec_unchecked(mut v: Vec<u8>) -> CString {
|
||||||
/// to undefined behavior or allocator corruption.
|
/// to undefined behavior or allocator corruption.
|
||||||
///
|
///
|
||||||
/// [`into_raw`]: #method.into_raw
|
/// [`into_raw`]: #method.into_raw
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// Create a `CString`, pass ownership to an `extern` function (via raw pointer), then retake
|
||||||
|
/// ownership with `from_raw`:
|
||||||
|
///
|
||||||
|
/// ```no_run
|
||||||
|
/// use std::ffi::CString;
|
||||||
|
/// use std::os::raw::c_char;
|
||||||
|
///
|
||||||
|
/// extern {
|
||||||
|
/// fn some_extern_function(s: *mut c_char);
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// let c_string = CString::new("Hello!").unwrap();
|
||||||
|
/// let raw = c_string.into_raw();
|
||||||
|
/// unsafe {
|
||||||
|
/// some_extern_function(raw);
|
||||||
|
/// let c_string = CString::from_raw(raw);
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
#[stable(feature = "cstr_memory", since = "1.4.0")]
|
#[stable(feature = "cstr_memory", since = "1.4.0")]
|
||||||
pub unsafe fn from_raw(ptr: *mut c_char) -> CString {
|
pub unsafe fn from_raw(ptr: *mut c_char) -> CString {
|
||||||
let len = libc::strlen(ptr) + 1; // Including the NUL byte
|
let len = libc::strlen(ptr) + 1; // Including the NUL byte
|
||||||
|
|
Loading…
Reference in a new issue