Why str is a builtin?

As far as I understand, str type can be implemented as a regular DST struct:

struct Str([u8]);

Here (play) I have implemented a couple of functions for that struct, and seems like it is possible.

Any reason why str is a compiler builtin and not a library type, except for historical reasons?

Historical reasons mostly.

AFAIK, the only thing tying str to the language is string literals (both in expressions and in patterns), so str would still has to be a lang item even if moved to the library.
Unfortunately, simply moving str to the library now is a breaking change, because built-in types are subject to some special weird name resolution rules.

If you search PRs you can find the grisly details. I’m on mobile or I’d link you.

Actually, I’m a fan of creating a library alias for str, promoting it the documentation and phasing out str itself eventually. This is probably completely crazy, because the library team never reacted on the issue linked below.


PR 19612. So it was even implemented.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.