Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 40 additions & 2 deletions library/core/src/ffi/c_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -651,8 +651,8 @@ impl CStr {
#[must_use = "this does not display the `CStr`; \
it returns an object that can be displayed"]
Comment thread
clarfonthey marked this conversation as resolved.
#[inline]
pub fn display(&self) -> impl fmt::Display {
crate::bstr::ByteStr::from_bytes(self.to_bytes())
pub fn display(&self) -> Display<'_> {
Display { c_str: self }
}

/// Returns the same string as a string slice `&CStr`.
Expand Down Expand Up @@ -847,3 +847,41 @@ impl Iterator for Bytes<'_> {

#[unstable(feature = "cstr_bytes", issue = "112115")]
impl FusedIterator for Bytes<'_> {}

/// Helper struct for safely printing a [`CStr`] with [`format!`] and `{}`.
///
/// A [`CStr`] might contain non-Unicode data. This `struct` implements the
/// [`Display`] trait in a way that mitigates that. It is created by the
/// [`display`](CStr::display) method on [`CStr`]. This may perform lossy
/// conversion, depending on the platform. If you would like an implementation
/// which escapes the [`CStr`] please use [`Debug`] instead.
///
/// # Examples
///
/// ```
/// #![feature(cstr_display)]
///
/// let s = c"Hello, world!";
/// println!("{}", s.display());
/// ```
///
/// [`Display`]: fmt::Display
/// [`format!`]: ../../../std/macro.format.html
#[unstable(feature = "cstr_display", issue = "139984")]
pub struct Display<'a> {
Comment thread
clarfonthey marked this conversation as resolved.
c_str: &'a CStr,
}

#[unstable(feature = "cstr_display", issue = "139984")]
impl fmt::Debug for Display<'_> {

@Darksonn Darksonn Sep 16, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The debug impl on the return value of CStr::display() is new, untested behavior. Should we have a test about what this outputs?

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No equivalent tests for os_str::Display and path::Display, so, it should be fine.

fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.c_str, f)
}
}

#[unstable(feature = "cstr_display", issue = "139984")]
impl fmt::Display for Display<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(crate::bstr::ByteStr::from_bytes(self.c_str.to_bytes()), f)
}
}
2 changes: 2 additions & 0 deletions library/std/src/ffi/c_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ pub use alloc::ffi::c_str::IntoStringError;
pub use alloc::ffi::c_str::{CString, NulError};
#[stable(feature = "rust1", since = "1.0.0")]
pub use core::ffi::c_str::CStr;
#[unstable(feature = "cstr_display", issue = "139984")]
pub use core::ffi::c_str::Display;
#[stable(feature = "cstr_from_bytes_until_nul", since = "1.69.0")]
pub use core::ffi::c_str::FromBytesUntilNulError;
#[stable(feature = "cstr_from_bytes", since = "1.10.0")]
Expand Down
Loading