5 ms·
So, there's like, a few things here. First is, technically they both do use the same notation, &T, where T=str and T=[u8]. This is the whole "unsized types" thi
by steveklabnik 6y ago
So, there's like, a few things here. First is, technically they both do use the same notation, &T, where T=str and T=[u8]. This is the whole "unsized types" thing. &Path is another example of this, String : &str :: PathBuf : &Path.
Beyond that though, &[T] implies a slice of Ts, that is, multiple Ts in a row. But a &str is a slice of a single string. So &[str] would feel wrong; that is, a &str is a slice of a String or another &str or something else, but isn't like, a list of multiple things. It's String, not Vec<str>.
Basically, Strings are just weird.
- nicklecompte 6y agoIn particular: strings aren’t actually simple arrays of characters in Rust like they are in C, but there is an underlying array on the heap, and the notion of “slicing” that array of characters still makes sense semantically.
- 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.
- cryptonector 6y agoSlicing strings is a difficult concept when dealing with Unicode because slice indices and lengths can come in multiple flavors, but the only flavor that makes sense (grapheme or grapheme cluster) is difficult to count.
- scoutt 6y ago>strings aren’t actually simple arrays of characters in Rust like they are in C, but there is an underlying array on the heap Not a Rust expert, but to my understanding, 'string' is an array of characters, not necessarily living in the heap. The object 'String' (with capital S) might be, but with &str I can have a constant array of characters that is not in the heap and not even in the stack: it's in the code (.text code segment[1]?, or EEPROM or flash, for embedded folks). If I understood it correctly, &str is a slice with a pointer to that piece of code. Of course being const, it cannot be changed. I can copy it into a String and manipulate it (append, insert, etc.), and that's where the heap is used. &str, as an hypothetical struct, it may be in the stack, initialized in-scope with (probably) a pointer to somewhere in .text plus some bytes for length or other information. [1] https://en.wikipedia.org/wiki/Code_segment https://en.wikipedia.org/wiki/Code_segment
- deleted 6y ago[deleted]
- brundolf 6y ago&str is not necessarily a static string literal; it's a slice of any string, living anywhere (including on the heap inside a String). Technically speaking, it can't be mutated because it's a non-mutable reference (like any other non-mutable reference). &mut str is a thing that can exist, and is mutable, it's just that you can't get one of those for a static (literal) string that lives in the code.
- scoutt 6y agoThanks. Yes, of course it can point to something in the heap. My comment was related to GP's "but there is an underlying array on the heap" and I wanted to say that &str is not always necessarily in the heap. I missed that part.
- adkadskhj 6y agoYea but i don't think that's what GP was asking, imo. Rather than `[str]`, i think they were asking why it's `str` and not `[char]`, no? Just as `[u8]` is to `Vec<u8>`, `[char]` is hypothetically to `Vec<char>`.. and `Vec<char>` is basically a `String`, no? _edit_: Though looking at the docs, `char` is 4 always bytes, so i guess that's where the breakdown would be? `char` would need to be unsized i guess, but then it would be an awkward `[unsized_char]`, which is like two unsized types.... hence `str` maybe?
- codys 6y ago`char` is a (internally) a `u32` because it represents any single unicode character. `str` is not a `[char]`, because rust doesn't store strings as utf-32 (system APIs don't accept utf-32, and it tends to waste space in many cases) `str`'s data layout happens to be `[u8]`, but it's type provides additional guarantees about the structure of the data within it's internal `[u8]` (for example, forbidding sequences of u8 that don't encode valid utf-8).
- steveklabnik 6y agoTo be extra pedantic, char represents a single Unicode Scalar Value.
- erik_seaberg 6y agoThis is a big deal because adding an accent mark to a letter (often) means a single char can no longer store it. APIs should not orient around isolated scalar values or codepoints because most devs will misuse them, not being experts on combining and normalization.
- adkadskhj 6y agoWell yea, i wasn't saying `[char]` _is_ a `str`, rather i was positing that the GP comment was asking why it's a `str` than some hypothetical `[unsized_char]`. I think `char` _would_ work, if it was similarly unsized like a single piece of `str`. The Problem is .. as i see it, that `[unsized_char]` seems odd.
- dralley 6y agoI wish they had been named &path and Path instead, it would feel more consistent than String, &str, PathBuf, &Path
- steveklabnik 6y agoI joke the only way that I'll sign off on Rust 2.0 is if we get to do the exact opposite, rename String to StrBuf, haha! (Same reason though, for consistency.)
- NanoCoaster 6y agoI'd actually love something like that, mostly because differentiating between "str" and "String" in, uh, speech can be pretty annoying. They both sound the same! :) I've taken to calling "String" "owned string" but that's a little unwieldly and not quite suited for complete Rust beginners who don't know the concept of ownership yet. Same with "string reference" for "str".
- deleted 6y ago[deleted]
- CJefferson 6y agoRelated question (if you don't mind another). Why is 'str' a "primitive type"? What about 'str' means it has to be primitive, instead of being a light-weight wrapper around a '&[u8]' (that obviously enforces UTF8 requirements as approriate).
- yazaddaruvala 6y agoYou can't just index into a string. let hello: &[u8] = "Héllo".as_u8_slice(); // hello[1] != "é", I believe it would be the bottom half-bytes of "H". // hello[2] != "é", I believe it would be the top half-bytes of "é". To use &[u8] would be very non-ergonomic.
- SAI_Peregrinus 6y agoOne could, in theory, make an ExtendedGraphemeCluster type, and make str a slice of ExtendedGraphemeClusters. So &[ExtendedGraphemeCluster] could be indexed into without having things not make sense. Of course that's much more complicated than most other primitives, and most people don't have any idea what an Extended Grapheme Cluster even is. But since they're the Unicode notion that most naturally maps to a "character" you could just call the type Character or char, and confuse the hell out of the C programmers by having a variable-width char type.
- josephg 6y agoSure - but iterating by extended graphemes isn’t the only thing you want to do with strings. Sometimes you want to treat them as a bunch of UTF-8 bytes. Sometimes you want to iterate / index by Unicode code points (eg for CRDTs). And sometimes you want to render them, however the system fonts group them. It makes sense to have a special type because it can supports all of this stuff through separate methods on the type. (Or through nearby APIs). It’s confusing, but I think it’s the right choice. Although, I think the most confusing thing about rust strings isn’t that &str isn’t &[u8]. It’s that &str isn’t just &String or something like that.
- 6y ago
- yazaddaruvala 6y agoAre there potentially other situations where `&[T + !Sized]` makes sense? The majority of the functions on `&str` seem to make sense for all `&[T + !Sized]` where `type str = [unsized_char]`.
- steveklabnik 6y agoI don't really know what unsized_char would even mean, chars have a size, and str is not a sequence of chars. That said, I'm also not sure in the general case.
- yazaddaruvala 6y agounsized_char would be some phantom/opaque type that is not actually useable because it represents an object of variable length. e.g. Think of it as an enumeration like : unsized_enum unsized_char { Char1(u8), Char2(u8, u8), Char3(u8, u8, u8), } let c: unsized_char = ...; // is a complied error: “cannot assign a variable stack size.” or something. let parsed: &[unsized_char] = unsafe { /* parse some bytes */}; // this complies because the implementation of some hypothetical &[T + !Sized] appropriately abstracts the allocated memory region. Unlike a normal enum it doesn’t always take 3 bytes of memory, it’s variable memory such that [unsized_char] might only take 1 byte if there is only 1 unsized_char::Char1 inside. Of course this means that you can’t index into the slice because the boundary is not clear. What I’m describing is not different than the internals of str. I’m just trying to armchair design a generic implementation such that str and slice are more unified.
- comex 6y agoYou’re right that what you’re suggesting could work as a way to treat &str as a type of slice, without breaking the type system. But there’s a fatal flaw. If you have `s: &[unsized_char]`, what should slicing, say, `&s[2..4]` do? With the actual `&str`, this treats 2 and 4 as byte indices. But if you’re going to treat str as a slice of a specific type, the indices really should be in units of that type. `&s[2..4]` should return the second and third grapheme clusters, or codepoints, or whatever you want to define `unsized_char` to be. The same applies to `len()` and all the other methods that return or accept indices. But if it’s in units of `unsized_char`, how do you actually implement indexing? You could do an O(n) scan from the beginning of the string, but that’s clearly unacceptable. Or you could set up some kind of acceleration data structure behind the scenes, but that would be expensive to maintain, and would conflict with Rust’s goals of explicitness and cheap FFI. If on the other hand you keep indexing with byte offsets, you end up with almost no operations that work, and do the same thing, for both &[unsized_char] and normal slices. You wouldn’t be able to write any useful generic code that operates on &[T] for T: ?Sized, at least not without branching on which kind of slice it is. And even from a human user’s perspective, the syntactic similarity between two things that work differently could confuse more than it helps. Again, a matter of explicitness.
- Covzire 6y agoAre they though? I've long wondered why the Rust team hasn't imitated C# or other languages ease of use with strings while also retaining the existing functionality for lower-level use cases. I suppose it's a kind of gauntlet that a Rust dev would have to go through which could be a good thing but personally hitting walls with strings really turned me off on Rust the first time I tried it simply because my expectations were diametrically opposed to the reality of strings in rust.
- steveklabnik 6y agoDifferent languages trade off different things. I don't know enough about C#'s string handling to reply effectively, but Rust does the best it can given the things it cares about. Other languages don't care about the same things, and therefore, can make different tradeoffs.