4 ms·
All of this is true, IF you assume that you want Unicode string. That, especially on system/embedded software (the kind of software that Rust is targeting) you
by alerighi 6y ago
All of this is true, IF you assume that you want Unicode string. That, especially on system/embedded software (the kind of software that Rust is targeting) you don't really care about Unicode and you can simply treat strings as array of bytes.
And I live in a country where you usually use Unicode characters. But for the purpose of the software that I write, I mostly stick with ASCII. For example I use strings to print debug messages to a serial terminal, or read commands from the serial terminal, or to put URL in the code, make HTTP requests, publish on MQTT topics... for all of these application I just use ASCII strings.
Even if I have to represent something on the screen... as long as I have a compiler that supports Unicode as input files (all do these days) I can put Unicode string constants in the code and even print them on screen. It's the terminal (or the GUI I guess, but I don't write software with a GUI) that translates the bytes that I send on the line as Unicode characters.
And yes, of course the length of the string doesn't correspond at the characters shown on the screen... but even with Unicode you cannot say that! You can count (and that what Rust does) how many Unicode code points you have, but a characters could be made of more code points (stupid example, the black emoji is composed by a code point that says "make the following black" and then the emoji itself).
So to me it's pointless, and I care more about knowing how many bytes a string takes and being able to index the string in O(1), or take pointers in the middle of the string (useful when you are parsing some kind of structured data), and so on.
In conclusion Rust is better when you have to handle Unicode string, but most application doesn't have to handle them, and handling them I don't mean passing them around as a black box, not caring how they contain (yes, in theory you should care about not truncating the string in the middle of a code point when truncating strings... in reality, how often do you truncate strings?)
- stouset 6y agoLiterally nothing is stopping you from using `&[u8]` for 7-bit ASCII.
- estebank 6y ago> So to me it's pointless, and I care more about knowing how many bytes a string takes and being able to index the string in O(1), or take pointers in the middle of the string (useful when you are parsing some kind of structured data), and so on. Which is why you can still sub-slice Strings[1] > Literally nothing is stopping you from using `&[u8]` for 7-bit ASCII. Not only that, there's first party support for literals that are that[2]. [1]: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=fe1fa17cd5bfbe5b7d1e4a162a8c106b https://play.rust-lang.org/?version=stable&mode=debug&editio... [2]: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=28b9b84ebf317f0e6bae4e00907239c3 https://play.rust-lang.org/?version=stable&mode=debug&editio...
- burntsushi 6y agoCan you explain more specifically how Rust's String/str type does not handle your ASCII case? They are just bytes and ASCII is a subset of UTF-8.
- pezezin 6y agoIt's funny that you mention MQTT, as the spec requires strings (including topic names) to be encoded in UTF-8: http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/cos02/mqtt-v3.1.1-cos02.html#_Toc398718016 http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/cos02/mqtt-v3.1.... Granted, ASCII is a subset of UTF-8, so as long as you control all the publishers and enforce the ASCII-only rule, you should be ok. But if some day you need to integrate third-party systems and they use characters outside of ASCII...