3 ms·
I did a basic test Rust program: load to strings certain files in a zip file. The first file in the archive happened to be UTF-16 instead of UFT-8 so a vanilla
by antaviana 8y ago
I did a basic test Rust program: load to strings certain files in a zip file. The first file in the archive happened to be UTF-16 instead of UFT-8 so a vanilla read_to_string for the file in the archive did not do the trick. I googled a bit and it seemed that, in order to be efficient, had to be done with unsafe code. Maybe I was very unlucky, but I was surprised that 5 minutes after trying rust for the very first time I had to use unsafe code.
- steveklabnik 8y agoWhy not https://doc.rust-lang.org/std/string/struct.String.html#method.from_utf16 https://doc.rust-lang.org/std/string/struct.String.html#meth... ?
- antaviana 8y agoI did not find a trivial way to go from the Vec<u8> returned by read_to_end to some kind of u16 container. Everything I found in Google involved unsafe code. Probably a function read_to_end returning a Vec<u16> would have been great.
- steveklabnik 8y agoAh yeah. It's tricky, but it also doesn't require unsafe: https://play.rust-lang.org/?gist=e261ed324a022ac82bb88adcee1c4a24&version=stable&mode=debug&edition=2015 https://play.rust-lang.org/?gist=e261ed324a022ac82bb88adcee1... I'm sure there's a package that makes this easy, but this took me about ten minutes to toss together from scratch.
- drbawb 8y agoAgh! Chunks is what I was looking for! I was trying to whip up an example[1] and was positive I had used a pair-wise iterator before. The best I came up w/ was zipping two iterators of the hi/lo bytes. -- I feel the parent's pain, because this is coming from someone who has been programming Rust since it had green threads and `~ / @` pointers. I love Rustdoc, but I really wish it wasn't so obtuse when it came to casually browsing the various iterator traits. Perhaps Steve, for the benefit of myself and thread parent, you could point us to some better-edited literature for the various iterator methods? P.S: just wanted to add, if I were faced w/ this issue in production code, I likely would've just reached for the excellent byteorder[2] crate. It's so useful for "bit twiddling" like this. [1]: https://play.rust-lang.org/?gist=4b4ff7263d9744caeb8bacd4c0d6aeb0&version=stable&mode=debug&edition=2015 https://play.rust-lang.org/?gist=4b4ff7263d9744caeb8bacd4c0d... [2]: https://docs.rs/byteorder/1.2.6/byteorder/trait.ReadBytesExt.html#method.read_u16_into https://docs.rs/byteorder/1.2.6/byteorder/trait.ReadBytesExt...
- steveklabnik 8y agoAh yeah, byteorder would be the right call. > Perhaps Steve, for the benefit of myself and thread parent, you could point us to some better-edited literature for the various iterator methods? I used that page to find this. I knew it was "chunks" or "windows" or something, so I just looked at them (I guessed windows, but I was wrong).
- antaviana 8y agoOk, thanks I see. I suppose that it was a bit too much for a stackoverflow-prone noob like me, with only 5 minutes of rust flight time.
- steveklabnik 8y agoYeah, the big thing is how comfortable you are with bit twiddling. If you are, it's no big deal, but if you're not, it'd be much harder, for sure.