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?
The string data type is a non-primitive data type but it is predefined in java, some people also call it a special ninth primitive data type. This solves the case where a char cannot store multiple characters, a string data type is used to store the sequence of characters.
Primitive data types - includes byte , short , int , long , float , double , boolean and char. Non-primitive data types - such as String , Arrays and Classes (you will learn more about these in a later chapter)
String is non-primitive because only class can have methods. Primitive can not. And String need many functions to be called upon while processing like substring, indexof, equals, touppercase. It would not have been possible without making it class.
In JavaScript if the data is not an object and has no methods and properties is called as a primitive data type or a primitive value. The different types of primitive data types in JavaScript are string, number, Boolean, undefined, symbol, null and big int.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With