-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix messaging around what is and isn't a "slice" AKA. stop using "slice" to mean &[T] #101353
Comments
In my opinion,
|
I don't think it's wrong. The english language has this common pattern where a base word forms a large category covering many related things. That category may have a central example but also contain some more tangential things. It's like "tea" can mean a beverage made from camellia sinensis, the plant itself but also many kinds of herbal teas. |
Historical note: Before DSTs,
That said, I agree that consistency and formality in the official documentation is better, and also call |
Aside, but your link "4" mentions calling &[T] a "borrowed slice", which I think is quite a good name and one that I didn't mention in the OP |
I do think this is confusing, especially for some beginners. Here are some discussions about the slice type with my friends:
After this discussion, I think the docs of slices are kind of confusing. And would like to open an issue about the docs. Then I reached this related issue. I tried my best to explain the difference between It may not be too bad to mix struct Foo {
sized: u32,
dst: [u8],
} It will be extremely confusing if we mix the |
I think the most confusing point may be: |
Slices - Why the messaging is wrong / confusing and needs to be clarified
In my experience teaching newbies about rust (mostly on the community discord and in person) there has always been a speed-bump in the learning process, and that's slices.
The way we talk about slices is confusing and ambiguous - we first (IMO incorrectly) teach that
&[T]
is called a slice, but then later when we introduce unsized types and reveal that actually[T]
is a slice, but also continue to call&[T]
a slice in extensive documentation.Things become even more confusing when some documentation also uses the terms "shared slice" and "mutable slice", or defines a slice as a "view into a contiguous sequence" (which IMO conveys a sense of indirection?). Things that add to the confusion:
Box<[T]>
and even the rareBox<str>
[T]
/str
as a generic parameter (why is or isn't it&[T]
/&str
?).[T]
relate to[T; N]
ie. (if&[T]
is a slice then is&[T; N]
an array?)Due to the langage we use to teach, rust beginners must learn and then unlearn the terminology several times before they arrive at the correct nuances of how slices work and in what contexts we use what terms. I believe this should change.
While I believe using "slice" to mean
&[T]
informally is acceptable, I think having this language present in documentation does the community a disservice and makes everything more difficult than it needs to be.How we could fix it
We need to focus on accurately teaching the language, and using the terminology consistently:
[T]
is a slice - an unsized type that needs to be behind some form of pointer / reference / indirection&[T]
is a slice reference / slice ref - a fat pointer which includes the length of the slice.&mut [T]
should be called a mutable slice ref&str
- what should we call this? a "string ref" / "str ref" maybe? currently it is often called a "string slice" but that is inaccurate...These basics should not be conflated - especially in documentation. Make it clear which ones are slices and which ones are references. (yes, I know that it's more words to type out)
&[T]
a "shared slice" is confusing since it implies that the type is a kind of slice, when in fact it is a reference to a slice..as_slice()
and.as_mut_slice()
, while technically incorrect (they return slice references) should be fine to stay since it's still fairly clear what the user is getting back.If we really want to continue calling
&[T]
a "slice" and&mut [T]
a "mutable slice", then we must come up with a new name for a[T]
to avoid ambiguous language.Locations (not comprehensive)
primitive slice
std::slice
std::slice::from_raw_parts
returns a "slice"std::slice::from_raw_parts_mut
returns a "mutable slice"&[T]
"A slice is dynamically-sized view into a contiguous sequence, written as [T].
It is often seen in its borrowed forms, either mutable or shared. The shared slice type is &[T], while the mutable slice type is &mut [T], where T represents the element type." - This is the best description so far, but again, the reference type names are used interchangably with an actual slice type.
&mut [T]: a 'mutable slice'. It mutably borrows the data it points to.
Box<[T]>: a 'boxed slice'"
TL;DR: Using "slice" to mean
&[T]
is bad, confusing, and hard to learn.Does anyone else think this is a problem? Please bikeshed your
&[T]
and&mut [T]
names and other thoughts below :)The text was updated successfully, but these errors were encountered: