Hi,
this is my first time using bindgen to make use of an existing c library. That library has a function that returns a c-style string as an const char *
(in C), which bindgen converts to *const ::std::os::raw::c_char
(Rust). For now, i want to print!()
that string out to the console, and later do more stuff with it. What should i convert it to? If i understand the C library correctly, the string it returns is actually a constant that is compiled into the program, so the pointer will not be on the heap and should not be free()
'd. Which of the rust string types works in a way that the memory will not be freed when an instance goes out of scope? So what should i use? I have looked at:
-
CString
seems to be the obvious choice due to naming, but i'm not sure whether it will try to own the pointer andfree()
it when going out of scope? -
&str
seems like a good choice because it's the type a static string would have in rust as well. how would i convert to this, since afaik&str
is a fat pointer containing the string length (and the c string is null terminated). At this point, the string returned by the function is ASCII-only, but what should i think about when that is not guaranteed? -
String
- i won't mind copying the content to an owned String, since it's not very long, not in any performace critical path, etc, so i'd use this in case it's less of a hassle than the other options. How would i do the conversion correctly?
Kind regards