File tree 1 file changed +21
-0
lines changed
1 file changed +21
-0
lines changed Original file line number Diff line number Diff line change @@ -287,6 +287,27 @@ impl CString {
287
287
/// to undefined behavior or allocator corruption.
288
288
///
289
289
/// [`into_raw`]: #method.into_raw
290
+ ///
291
+ /// # Examples
292
+ ///
293
+ /// Create a `CString`, pass ownership to an `extern` function (via raw pointer), then retake
294
+ /// ownership with `from_raw`:
295
+ ///
296
+ /// ```no_run
297
+ /// use std::ffi::CString;
298
+ /// use std::os::raw::c_char;
299
+ ///
300
+ /// extern {
301
+ /// fn some_extern_function(s: *mut c_char);
302
+ /// }
303
+ ///
304
+ /// let c_string = CString::new("Hello!").unwrap();
305
+ /// let raw = c_string.into_raw();
306
+ /// unsafe {
307
+ /// some_extern_function(raw);
308
+ /// let c_string = CString::from_raw(raw);
309
+ /// }
310
+ /// ```
290
311
#[ stable( feature = "cstr_memory" , since = "1.4.0" ) ]
291
312
pub unsafe fn from_raw ( ptr : * mut c_char ) -> CString {
292
313
let len = libc:: strlen ( ptr) + 1 ; // Including the NUL byte
You can’t perform that action at this time.
0 commit comments