Skip to content

Need to stress that byte-string literals are not null terminated #39295

@pnkfelix

Description

@pnkfelix

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsP-mediumMedium priority

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions