Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is `str` encapsulated inside `String` instead of inside a `Box<str>`?

It's not causing me any difficulties — I am perfectly capable of using String — but is there any reason that str is encapsulated in its own special type rather than inside the more general Box type? If there is a reason then the answer might help me model how to work with Box differently.

Why is str encapsulated inside String instead of inside a Box<str>? Is it simply for convenience of typing such a common structure or is there a deeper reason?

like image 256
Lemma Prism Avatar asked Dec 14 '22 10:12

Lemma Prism


1 Answers

String is more like a Vec<char> than a Box<str> - it has methods to push more chars on the end, or push a whole str. It has length and capacity, rather than only length. Like Box and Vec, it owns it's contents, and places them on the heap; unlike Box, it also extends the functionality of str beyond its inherent properties.

like image 159
Zarenor Avatar answered Dec 19 '22 12:12

Zarenor