Hacker News new | past | comments | ask | show | jobs | submit login

>String is dynamic data on the heap that you own and can modify. str is some data somewhere that you can’t modify.

This is not the definition. You can modify both. Being able to modify something depends on whether you can do something with a &mut reference to it, and both &mut String and &mut str provide methods for modifying them.

The difference between the two types is just that String owns its allocation while str doesn't. So modifying a String is allowed to change its bytes as well as add and remove bytes, the latter because the String owns its allocation. Modifying a str only allows changing its bytes.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: