Skip to content

Confusing use of the term "undefined behavior" in the note of type.str.value #2266

@ia0

Description

@ia0

The note of type.str.value uses the term "undefined behavior" for what is usually denoted as "library undefined behavior" (which is by the way not a concept in the reference):

r[type.str.value]
A value of type `str` is represented in the same way as `[u8]`, a slice of 8-bit unsigned bytes.
> [!NOTE]
> The standard library makes extra assumptions about `str`: methods working on `str` assume and ensure that the data it contains is valid UTF-8. Calling a `str` method with a non-UTF-8 buffer can cause [undefined behavior] now or in the future.

I believe this is confusing. The term "undefined behavior" in the reference should only be used for what is usually denoted as "language undefined behavior" since the reference is about the language. It is explicitly said in the introduction (under "what the reference is not") that the standard library is not documented by the reference:

This book also does not serve as a reference to the [standard library] included in the language distribution.
Those libraries are documented separately by extracting documentation attributes from their source code.
Many of the features that one might expect to be language features are library features in Rust, so what you're looking for may be there, not here.

Unless there is already a plan to talk about library UB in the reference (which I guess would be restricted to notes), I believe it would be preferable to avoid the terminology "undefined behavior" for library UB, and use a more evasive terminology, like "calling a str method with a non-UTF-8 buffer violates assumptions and invariants of the standard library and could result in unexpected behaviors down the line".

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions