diff options
| author | Corey Farwell <coreyf@rwell.org> | 2017-06-18 18:22:54 -0700 |
|---|---|---|
| committer | Corey Farwell <coreyf@rwell.org> | 2017-06-20 13:49:42 -0400 |
| commit | 93abc2f87793e1b5a4e7b87c6a47629270be6ad9 (patch) | |
| tree | e73ecd2e03fcf16aea665d08a99b9658b08102c1 /src/libstd | |
| parent | d3c26fe7e32630470ed6ffc53cf65cdeaeecd9e9 (diff) | |
| download | rust-93abc2f87793e1b5a4e7b87c6a47629270be6ad9.tar.gz rust-93abc2f87793e1b5a4e7b87c6a47629270be6ad9.zip | |
Add doc example for `CString::from_raw`.
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/ffi/c_str.rs | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/src/libstd/ffi/c_str.rs b/src/libstd/ffi/c_str.rs index ca7cd37a88d..1a91417ca0e 100644 --- a/src/libstd/ffi/c_str.rs +++ b/src/libstd/ffi/c_str.rs @@ -287,6 +287,27 @@ impl CString { /// to undefined behavior or allocator corruption. /// /// [`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")] pub unsafe fn from_raw(ptr: *mut c_char) -> CString { let len = libc::strlen(ptr) + 1; // Including the NUL byte |
