10

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?

llogiq
  • 13,815
  • 8
  • 40
  • 72

1 Answers1

11

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.

Chris Morgan
  • 86,207
  • 24
  • 208
  • 215
  • Ah, I thought that it was due to the current solution being good enough. Still it seemed a bit off, as in many places the language was reduced by moving constructs into std, that's why I asked. – llogiq Jul 13 '15 at 12:30
  • 1
    See https://github.com/rust-lang/rust/issues/19036 and https://github.com/rust-lang/rust/pull/19612 – Steve Klabnik Jul 13 '15 at 15:20
  • String literals *and* pattern matching! – bluss Jul 13 '15 at 16:31