Convert [::std::os::raw::c_char; 256usize] to string?

  1. Context: https://github.com/rust-accel/accel/blob/bd31ba1573a35bbb195d0412a89d453e89cceb5b/cuda-sys/src/cudart.rs#L430

  2. How do I convert the field

    pub name: [::std::os::raw::c_char; 256usize],

My goal is to format! it. I've already tried "{}" and "{:?}"

https://stackoverflow.com/questions/33036859/why-does-println-work-only-for-arrays-with-a-length-less-than-33

You can of course replace println! by format!

CStr::from_bytes_with_nul

There are two different interpretations of [c_char; 256] here:

  • An array of bytes. You thus wish to print it as an array ... of bytes, e.g., [0x42, 0x18, 0x0, ... ].
    In that case you need to implement the logic format yourself, with a new type.

  • A C String with a capacity (max len) of 256. In which case, as @DanielKeep pointed out, you can use CStr to Debug::fmt it. This is what I would do. Sadly it requires (carefully) using unsafe to get a copy-only-if-needed-to-append-a-null-byte-without-redundant-scans implementation, with the annoying part of CStr API using u8 instead of c_chars.