Why str is a builtin?


#1

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?


#2

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.


#3

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


#4

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.

https://github.com/rust-lang/rfcs/issues/1208


#5

PR 19612. So it was even implemented.