4 ms·
Rust’s native strings are not null terminated, though there are alternative string types (CString, CStr) that are. If you want to call a function that requires
by steveklabnik 3y ago
Rust’s native strings are not null terminated, though there are alternative string types (CString, CStr) that are. If you want to call a function that requires a zero terminated string, you’ll have to convert if you don’t have one already.
And yep, the latter is true. Just how it is. It’s unfortunate, but it’s also inevitable.
- hi-v-rocknroll 3y agoDifferent flavors for different use-cases. There was even $-terminated ones prevalent on DOS which got it from CP/M which got it from DEC OS/8. Pascal-style strings (length + data or length + pointer to data) have the nice properties of O(1) size inquiry, simpler bounds checking, and easier to represent strings that contain embedded NULs. A downside is though you have to pick an unsigned integer width of power of 2, usually 0 to 3. Another upside is (length + pointer to data) immutable substrings (with longer lifetimes that the parent string) are very cheap because they just point to wherever in the start and specify how much of the parent string with the length. There was a language IIRC that implemented a more complicated Pascal-style strings perhaps for COW or reference counting of the underlying allocation.