Is rust built-in string null terminated?

I do not understand how it matters. If the documentation does not say it is guaranteed to be null terminated, you must operate under the assumption that it isn't. Indeed @steffahn has pointed out that your own example does not always show a null-terminated string. People have pointed out why it cannot be null-terminated. Why would you need an explicit statement that says it isn't? What does that buy you?

7 Likes

The str type reference says:

A &str is made up of two components: a pointer to some bytes, and a length.

std::str::from_utf8 reference contains examples of converting sequences of bytes to &str.

Is this not good enough? Rust doesn't have a formal specification, so this is as good as you get. The semantics of string literals are supposed to be documented here but that page isn't finished and just says:

Note : This section is incomplete.

The NUL character is a valid Unicode (and ASCII) control character that can appear in the middle of Unicode text, so it's no good as a terminator. It's just a strange quirk of the C language.

This guy considers it "The Most Expensive One-byte Mistake".

6 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.