I'd like to be able to have const char *foo_struct_get_some_string (FooStruct*foo) that doesn't allocate, where FooStruct is defined in Rust.
First, I'd like to just hard assume that Rust uses malloc, so I don't need to export separate free functions. That's a even easier now with System in std::alloc - Rust though AIUI since we're building our Rust code as a static library it's the default.
Now, for avoiding allocating string copies - would using CString in our Rust structures be a best practice here? I'm looking at what Firefox does, although there's obviously quite a lot of code there.
If you also want to have str like properties on the Rust side, I think you need a type that combines guarantees of both string kinds: valid UTF-8 and trailing NUL without embedded NULs. Then they can deref to &str and you can also hand out pointers to the C side.
It is probably best to avoid having free in a difference code base than alloc. Even though explicit free functions add extra headache to codding. It maybe making your code brittle.
Only whispers of advice I have read from others rather than from a definitive source. My quick search just now brings up this;
"loaded dynamic-link libraries (DLLs) may create and use separate heaps"