Why does Rust have so many string representations?

The String and &str types hold UTF-8 strings. Filenames on most platforms are not required to be valid UTF-8, so if Rust would use the standard string types for filenames, you could not access some files, and you couldn't even list them.

The two OsStringExtvariants are not types. They are traits with platform-specific extensions for the OsString type.

The CString type is used for interacting with C code. C strings have different guarantees than Rust strings. specifically they are nul-terminated, and don't have any nul bytes in the interior of the string, but they are not required to be valid UTF-8.

On Unix platforms, CString and OsString could be combined into a single type, but on different platforms they need to be seperate due to incompatible requirements (e.g. on Windows, but don't ask me about the details).

8 Likes