-
Notifications
You must be signed in to change notification settings - Fork 13.9k
Description
I noticed in another project that someone had ported some C code, and I think they assumed that the best analogue for a C string literal was a Rust ASCII byte string literal.
The problem with this assumption is that C string literals are implicitly null-terminated. In C, "Hello" represents a sequence of six bytes: ['H', 'e', 'l', 'l', 'o', '\0']. But in Rust, b"Hello" represents just the five bytes ['H', 'e', 'l', 'l',' o'].
And that representation runs deep: if you cast the byte string reference to an unsafe pointer and use that to read one byte past the 'o', chances are you will not see a '\0'. (At least, that is my observation.)
It is too easy for a new user to make the mistake of equating these two things. We could add usage notes pointing this out, and advising people to add the '\0' when transcribing C string literals to Rust byte strings.