[rust-dev] strings, slices and nulls
banderson at mozilla.com
Wed Apr 18 13:51:10 PDT 2012
On 04/18/2012 12:43 PM, Niko Matsakis wrote:
> On 4/18/12 12:17 PM, Brian Anderson wrote:
>> I would kind of like for Rust strings not to expose the fact that they
>> are null-terminated (or just not be null terminated) but it seems
> I can see the practicality of null-terminating, but I would really like
> to preserve the flexibility to reverse that decision in the future.
> Forcing people to go through str::as_bytes() to observe the null
> terminating seems like a win—except that I'd probably call it as_cstr()
> or something like that, to emphasize it's null-terminated-ness.
I was considering that the behavior of as_bytes is a little surprising
because it exposes the null-terminator. We also already have as_c_str,
which is built off of as_bytes. (Also we have as_buf). Ideally only
as_c_str exposes the null-terminater, but if that's the case then
there's no reason for as_bytes or as_buf and you should just use
str::bytes to get a fresh, non-null-terminated copy. They are all just
doing an unsafe cast so exposing the implementation is unavoidable.
More information about the Rust-dev