Skip to content

Commit ead974f

Browse files
authored
Rollup merge of rust-lang#141498 - tamird:cstr-example-noise, r=jhpratt
Use C-string literals to reduce boilerplate Reduce boilerplate in doctests by replacing fallible function calls with literals.
2 parents 6fa36ec + 7b5a079 commit ead974f

File tree

1 file changed

+7
-34
lines changed

1 file changed

+7
-34
lines changed

library/core/src/ffi/c_str.rs

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -511,13 +511,8 @@ impl CStr {
511511
/// # Examples
512512
///
513513
/// ```
514-
/// use std::ffi::CStr;
515-
///
516-
/// let cstr = CStr::from_bytes_with_nul(b"foo\0").unwrap();
517-
/// assert_eq!(cstr.count_bytes(), 3);
518-
///
519-
/// let cstr = CStr::from_bytes_with_nul(b"\0").unwrap();
520-
/// assert_eq!(cstr.count_bytes(), 0);
514+
/// assert_eq!(c"foo".count_bytes(), 3);
515+
/// assert_eq!(c"".count_bytes(), 0);
521516
/// ```
522517
#[inline]
523518
#[must_use]
@@ -533,19 +528,8 @@ impl CStr {
533528
/// # Examples
534529
///
535530
/// ```
536-
/// use std::ffi::CStr;
537-
/// # use std::ffi::FromBytesWithNulError;
538-
///
539-
/// # fn main() { test().unwrap(); }
540-
/// # fn test() -> Result<(), FromBytesWithNulError> {
541-
/// let cstr = CStr::from_bytes_with_nul(b"foo\0")?;
542-
/// assert!(!cstr.is_empty());
543-
///
544-
/// let empty_cstr = CStr::from_bytes_with_nul(b"\0")?;
545-
/// assert!(empty_cstr.is_empty());
531+
/// assert!(!c"foo".is_empty());
546532
/// assert!(c"".is_empty());
547-
/// # Ok(())
548-
/// # }
549533
/// ```
550534
#[inline]
551535
#[stable(feature = "cstr_is_empty", since = "1.71.0")]
@@ -569,10 +553,7 @@ impl CStr {
569553
/// # Examples
570554
///
571555
/// ```
572-
/// use std::ffi::CStr;
573-
///
574-
/// let cstr = CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed");
575-
/// assert_eq!(cstr.to_bytes(), b"foo");
556+
/// assert_eq!(c"foo".to_bytes(), b"foo");
576557
/// ```
577558
#[inline]
578559
#[must_use = "this returns the result of the operation, \
@@ -598,10 +579,7 @@ impl CStr {
598579
/// # Examples
599580
///
600581
/// ```
601-
/// use std::ffi::CStr;
602-
///
603-
/// let cstr = CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed");
604-
/// assert_eq!(cstr.to_bytes_with_nul(), b"foo\0");
582+
/// assert_eq!(c"foo".to_bytes_with_nul(), b"foo\0");
605583
/// ```
606584
#[inline]
607585
#[must_use = "this returns the result of the operation, \
@@ -623,10 +601,8 @@ impl CStr {
623601
///
624602
/// ```
625603
/// #![feature(cstr_bytes)]
626-
/// use std::ffi::CStr;
627604
///
628-
/// let cstr = CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed");
629-
/// assert!(cstr.bytes().eq(*b"foo"));
605+
/// assert!(c"foo".bytes().eq(*b"foo"));
630606
/// ```
631607
#[inline]
632608
#[unstable(feature = "cstr_bytes", issue = "112115")]
@@ -645,10 +621,7 @@ impl CStr {
645621
/// # Examples
646622
///
647623
/// ```
648-
/// use std::ffi::CStr;
649-
///
650-
/// let cstr = CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed");
651-
/// assert_eq!(cstr.to_str(), Ok("foo"));
624+
/// assert_eq!(c"foo".to_str(), Ok("foo"));
652625
/// ```
653626
#[stable(feature = "cstr_to_str", since = "1.4.0")]
654627
#[rustc_const_stable(feature = "const_cstr_methods", since = "1.72.0")]

0 commit comments

Comments
 (0)