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

I’m not sure why it’s counterintuitive that &str and String are different things. Do you also find it counterintuitive in C++ that std::string is different from const char* ? What about &[u8] and Vec<u8> ?



Better analogy is std::string_view vs std::string


Technically that's a bit closer, yes, but way more people have heard of char* than string_view, and char* is similar _enough_ to &str that the analogy still works.


Nah. &str is const char* exactly. It's as primitive as types in rust get.


Nope. `&str` includes the length of the slice, which `const char*` does not. `std::string_view` is the proper analogy.


Another difference is that &str is guaranteed to be utf-8, whereas const char* can be any encoding (or no encoding).


Also, &str is closer to const uint8_t* than it is to const char*. Chars are signed by default and are at least 8 bits, but can be wider.




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

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

Search: