llogiq
llogiq

Reputation: 14511

Why is `str` a primitive type?

Looking at both the docs and the code, it appears that str is a primitive type, while String is a struct { Vec<u8> }. Now as str is to a [u8] what String is to a Vec<u8>, couldn't str have been defined as

struct str { slice: [u8]; }

similar to how AsciiStr is defined? Why was/is it (still?) defined as primitive?

Upvotes: 10

Views: 453

Answers (1)

Chris Morgan
Chris Morgan

Reputation: 90752

Once dynamically sized types came along, there no longer remained any good reason for str to be a primitive type; it could entirely reasonably have become a structure as you indicate, with a lang item for the benefit of string literals. But there didn’t seem any especially good reason to change it either (though the possibility was discussed a few times), and so the status quo remained.

Upvotes: 11

Related Questions