@@ -288,6 +288,26 @@ impl CString {
288
288
/// Failure to call [`from_raw`] will lead to a memory leak.
289
289
///
290
290
/// [`from_raw`]: #method.from_raw
291
+ ///
292
+ /// # Examples
293
+ ///
294
+ /// ```
295
+ /// use std::ffi::CString;
296
+ ///
297
+ /// let c_string = CString::new("foo").unwrap();
298
+ ///
299
+ /// let ptr = c_string.into_raw();
300
+ ///
301
+ /// unsafe {
302
+ /// assert_eq!(b'f', *ptr as u8);
303
+ /// assert_eq!(b'o', *ptr.offset(1) as u8);
304
+ /// assert_eq!(b'o', *ptr.offset(2) as u8);
305
+ /// assert_eq!(b'\0', *ptr.offset(3) as u8);
306
+ ///
307
+ /// // retake pointer to free memory
308
+ /// let _ = CString::from_raw(ptr);
309
+ /// }
310
+ /// ```
291
311
#[ stable( feature = "cstr_memory" , since = "1.4.0" ) ]
292
312
pub fn into_raw ( self ) -> * mut c_char {
293
313
Box :: into_raw ( self . into_inner ( ) ) as * mut c_char
@@ -311,6 +331,16 @@ impl CString {
311
331
///
312
332
/// The returned buffer does **not** contain the trailing nul separator and
313
333
/// it is guaranteed to not have any interior nul bytes.
334
+ ///
335
+ /// # Examples
336
+ ///
337
+ /// ```
338
+ /// use std::ffi::CString;
339
+ ///
340
+ /// let c_string = CString::new("foo").unwrap();
341
+ /// let bytes = c_string.into_bytes();
342
+ /// assert_eq!(bytes, vec![b'f', b'o', b'o']);
343
+ /// ```
314
344
#[ stable( feature = "cstring_into" , since = "1.7.0" ) ]
315
345
pub fn into_bytes ( self ) -> Vec < u8 > {
316
346
let mut vec = self . into_inner ( ) . into_vec ( ) ;
@@ -323,6 +353,16 @@ impl CString {
323
353
/// includes the trailing nul byte.
324
354
///
325
355
/// [`into_bytes`]: #method.into_bytes
356
+ ///
357
+ /// # Examples
358
+ ///
359
+ /// ```
360
+ /// use std::ffi::CString;
361
+ ///
362
+ /// let c_string = CString::new("foo").unwrap();
363
+ /// let bytes = c_string.into_bytes_with_nul();
364
+ /// assert_eq!(bytes, vec![b'f', b'o', b'o', b'\0']);
365
+ /// ```
326
366
#[ stable( feature = "cstring_into" , since = "1.7.0" ) ]
327
367
pub fn into_bytes_with_nul ( self ) -> Vec < u8 > {
328
368
self . into_inner ( ) . into_vec ( )
@@ -332,6 +372,16 @@ impl CString {
332
372
///
333
373
/// The returned slice does **not** contain the trailing nul separator and
334
374
/// it is guaranteed to not have any interior nul bytes.
375
+ ///
376
+ /// # Examples
377
+ ///
378
+ /// ```
379
+ /// use std::ffi::CString;
380
+ ///
381
+ /// let c_string = CString::new("foo").unwrap();
382
+ /// let bytes = c_string.as_bytes();
383
+ /// assert_eq!(bytes, &[b'f', b'o', b'o']);
384
+ /// ```
335
385
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
336
386
pub fn as_bytes ( & self ) -> & [ u8 ] {
337
387
& self . inner [ ..self . inner . len ( ) - 1 ]
@@ -341,6 +391,16 @@ impl CString {
341
391
/// includes the trailing nul byte.
342
392
///
343
393
/// [`as_bytes`]: #method.as_bytes
394
+ ///
395
+ /// # Examples
396
+ ///
397
+ /// ```
398
+ /// use std::ffi::CString;
399
+ ///
400
+ /// let c_string = CString::new("foo").unwrap();
401
+ /// let bytes = c_string.as_bytes_with_nul();
402
+ /// assert_eq!(bytes, &[b'f', b'o', b'o', b'\0']);
403
+ /// ```
344
404
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
345
405
pub fn as_bytes_with_nul ( & self ) -> & [ u8 ] {
346
406
& self . inner
0 commit comments