Skip to content

Commit 0362891

Browse files
authored
Rollup merge of #42470 - frewsxcv:frewsxcv/ffi-cstr-doc-examples, r=QuietMisdreavus
Add doc examples for `CString` methods. None
2 parents 03eb710 + 06f63f5 commit 0362891

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

src/libstd/ffi/c_str.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,26 @@ impl CString {
288288
/// Failure to call [`from_raw`] will lead to a memory leak.
289289
///
290290
/// [`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+
/// ```
291311
#[stable(feature = "cstr_memory", since = "1.4.0")]
292312
pub fn into_raw(self) -> *mut c_char {
293313
Box::into_raw(self.into_inner()) as *mut c_char
@@ -311,6 +331,16 @@ impl CString {
311331
///
312332
/// The returned buffer does **not** contain the trailing nul separator and
313333
/// 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+
/// ```
314344
#[stable(feature = "cstring_into", since = "1.7.0")]
315345
pub fn into_bytes(self) -> Vec<u8> {
316346
let mut vec = self.into_inner().into_vec();
@@ -323,6 +353,16 @@ impl CString {
323353
/// includes the trailing nul byte.
324354
///
325355
/// [`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+
/// ```
326366
#[stable(feature = "cstring_into", since = "1.7.0")]
327367
pub fn into_bytes_with_nul(self) -> Vec<u8> {
328368
self.into_inner().into_vec()
@@ -332,6 +372,16 @@ impl CString {
332372
///
333373
/// The returned slice does **not** contain the trailing nul separator and
334374
/// 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+
/// ```
335385
#[stable(feature = "rust1", since = "1.0.0")]
336386
pub fn as_bytes(&self) -> &[u8] {
337387
&self.inner[..self.inner.len() - 1]
@@ -341,6 +391,16 @@ impl CString {
341391
/// includes the trailing nul byte.
342392
///
343393
/// [`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+
/// ```
344404
#[stable(feature = "rust1", since = "1.0.0")]
345405
pub fn as_bytes_with_nul(&self) -> &[u8] {
346406
&self.inner

0 commit comments

Comments
 (0)