About OsString on Rust

Hello, I'm a rust beginner and I'm having a problem in Rust. When I don't use the std library, how do I access wchar_t? There is no OsString in alloc.

Why not use the std library? One reason is Windows driver programming, and the second reason is that the std library has requirements for system versions and cannot support older operating systems.

My understanding is that std is the layer at which Rust starts to depend on OS-specific details. OsString/OsStr are (as the names suggest) OS-specific, so they live in std and can't be used without it.

Now, aside from that, OsStr is not related to wchar_t. OsStr on Windows is WTF-8, not UCS-2/UTF-16 like NT expects. Every time I've had to deal with "native" Windows strings, I've ended up using some variation on Vec<u16> or &[u16] to avoid repeated conversions. There are also functions like String::from_utf16 and char::decode_utf16 for converting them into native Rust types (those functions are not OS-specific so are in alloc and core respectively)... but I don't believe there's a wide variation of CStr, so you'll need to handle null termination yourself.

6 Likes

thanks