How to create a C compatible static string array from &CStr s

⚓ Rust    📅 2026-03-22    👤 surdeus    👁️ 3      

surdeus

I need to have a C-style array of C strings to interface with a C library.

A &CStr seems to have a size of 16 (on 64bit systems), so placing them directly in an array doesn't create a valid array of pointers to C strings.

What I have come up with is the following:

struct CStrPtr(*const i8);

unsafe impl Sync for CStrPtr {}

static STRING_ARR: [CStrPtr; 2] = [
  CStrPtr(c"Item1".as_ptr()),
  CStrPtr(c"Item2".as_ptr())
]

I was wondering if there is a way to achieve this in a more ideomatic/non-unsafe way.

1 post - 1 participant

Read full topic

🏷️ Rust_feed