Closed steveklabnik closed 8 years ago
Thanks for the suggestion! I have no idea what this chapter will contain yet, but do think that both ways is important.
Thanks, Steve! From my point of view this chapter is very important for popularization of the language. We can't write everything in Rust right now, but if we can easily embed it - it is good to go.
p.s. I think that libsys::malloc and then writing bytes into could, potentially, be used similarly to the example №2 above.
I, probably, did not read existing documentaion properly, but it states that libraries use alloc_system by default, which means that we're free to use free
on C/C++ side, so no need in rs_drop mentioned above. It is also possible to add #![feature(alloc_system)]
to feel a bit safer.
I believe that better practical example of "Calling Rust code from C" shoud be provided. Especially returning dynamic string, because returning static string is not really practical.
I end up with two approaches but I doubt that any of them is somewhat valid
1) Allocate memory in C with malloc and write result in Rust
2) Return string and leak it to prevent from being dropped by rust, free by consumer