You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm not sure whether this is issue in standard library doc or in reference. I'm opening issue here, but feel free to close if this should be reported to reference instead.
The question is: what is slice?
Answer 1: "Slice is [T]"
Answer 2: "Slice is &[T] or &mut [T]"
Reference says:
A slice is a dynamically sized type representing a 'view' into a sequence of elements of type T. The slice type is written as [T].
Definitely, this means answer 1 is the right one.
However, slice type documentation says:
A dynamically-sized view into a contiguous sequence, [T]
Well, my English understanding is not good enough to tell what is [T] here: a "contigous sequence" itself or a view into one.
So I continue reading:
Slices are either mutable or shared. The shared slice type is &[T], while the mutable slice type is &mut [T] ... As slices store the length of the sequence they refer to, they have twice the size of pointers to Sized types
But this clearly suggests that correct answer to original question is answer 2!
To sum up, I can see three quotes related to the question. Quotes 1 and 3 look like contradicting, and second is maybe a bit complex to understand.
The text was updated successfully, but these errors were encountered:
MikailBag
added
the
A-docs
Area: Documentation for any part of the project, including the compiler, standard library, and tools
label
Feb 26, 2023
I think the answer to your first question is that it is both. A "slice" is [T], a "shared slice" is &[T], a "mutable slice" is &mut [T]. It looks like both std and the reference use the same terminology, so I'm not sure what isn't clear. The adjective is what distinguishes them. However, people may drop adjectives when in context there isn't any ambiguity. For example, for brevity, someone may say "slice of i32" instead of "a reference to a slice of i32".
I agree the first sentence in the std docs is a little awkward, and could be cleaned up.
It's also common to use the terminology "boxed slice" to refer to Box<[T]>, further reinforcing that the "slice" is the DST [T] rather than any particular reference into one. I suppose the fact that one can't (currently) directly own a DST leads to the implicit assumption that whenever a slice is being discussed, it will in practice be behind some reference/pointer; as ehuss said, this is distinction is usually clear (and intuitive) from context, though maybe we should be more precise in our terminology.
Location
https://doc.rust-lang.org/nightly/std/primitive.slice.html
https://doc.rust-lang.org/nightly/reference/types/slice.html
Summary
I'm not sure whether this is issue in standard library doc or in reference. I'm opening issue here, but feel free to close if this should be reported to reference instead.
The question is: what is slice?
Answer 1: "Slice is [T]"
Answer 2: "Slice is &[T] or &mut [T]"
Reference says:
Definitely, this means answer 1 is the right one.
However, slice type documentation says:
Well, my English understanding is not good enough to tell what is
[T]
here: a "contigous sequence" itself or a view into one.So I continue reading:
But this clearly suggests that correct answer to original question is answer 2!
To sum up, I can see three quotes related to the question. Quotes 1 and 3 look like contradicting, and second is maybe a bit complex to understand.
The text was updated successfully, but these errors were encountered: