Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is `str` a primitive type?

Tags:

internals

rust

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?

like image 825
llogiq Avatar asked Jul 13 '15 12:07

llogiq


People also ask

Why are strings primitive data types?

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.

Is string a primitive data type?

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)

Why is string not a primitive type?

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.

Why string is primitive in JavaScript?

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.


1 Answers

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.

like image 120
Chris Morgan Avatar answered Oct 02 '22 08:10

Chris Morgan