4 ms·
> strings aren’t actually simple arrays of characters in Rust Are you talking about them being a container with a pointer to the actual array, and also a size
by knodi123 6y ago
> strings aren’t actually simple arrays of characters in Rust
Are you talking about them being a container with a pointer to the actual array, and also a size and etc?
- Skunkleton 6y agochar is usually some single byte of data. Characters can be multiple bytes. Slicing a string on character boundaries is more coarse grained than slicing on byte boundaries.
- nicklecompte 6y agoRight - the point is that &str isn't syntactic sugar / alias / etc for &[u8] and it would be confusing to have a notation that suggested otherwise.
- seoaeu 6y agoIn C, strings can hold invalid unicode. However, in Rust a str is guaranteed to be valid utf-8. For added confusion, Rust has a `char` type which is actually 32-bits. You can create arrays of them, but the resulting string would be in utf-32 and thus incompatible with the normal `str` type.
- Animats 6y agolet s = "hello world".to_string(); for ch in s.chars() { print!({},ch); } will iterate through a string character by character. That's the most common use of the "char" type - one at a a time, not arrays of them. Although the proper grapheme form is: use unicode_segmentation::UnicodeSegmentation; // 1.7.1 let s = "hello world".to_string(); for gr in UnicodeSegmentation::graphemes(s.as_str(), true) { print!("{}", gr) } This will handle accented characters and emoji modifiers. A line break in the middle of a grapheme will mess up output. By the way, open season for proposing new emoji starts tomorrow.[1] [1] http://blog.unicode.org/2020/09/emoji-150-submissions-re-open-april-2.html http://blog.unicode.org/2020/09/emoji-150-submissions-re-ope...
- TheGrassyKnoll 6y agoThanks for the code snippet, however (kind of a Rust newbie here) $ cargo install unicode_segmentation (chokes) $ cargo install unicode-segmentation (seems to work) and in Cargo.toml [dependencies] unicode-segmentation = "1.7.1" (seems to work) yet in the code, its: use unicode_segmentation::UnicodeSegmentation; Why couldn't they be consistent using a dash vs. an underscore ?
- steveklabnik 6y agoYou don’t cargo install libraries; the cargo.toml line is the right thing. You can’t use -s in Rust identifiers, so they need to be normalized to _ to be referred to in code.
- estebank 6y agoIdentifiers in Rust can't have dashes (rustc will never rely on whitespace for proper tokenization). It is arguable that because of that crates should not have dashes in their name. cargo was changed to automatically convert dashes to underscores for convenience because some dislike underscores in names. I personally think this is an unnecessary source of confusion and now I would make all tools (mainly cargo) treat them interchangeably.