Skip to content

Slice type documentation contradicts the reference #108492

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

Open
MikailBag opened this issue Feb 26, 2023 · 3 comments
Open

Slice type documentation contradicts the reference #108492

MikailBag opened this issue Feb 26, 2023 · 3 comments
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools

Comments

@MikailBag
Copy link
Contributor

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:

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.

@MikailBag MikailBag added the A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools label Feb 26, 2023
@ehuss
Copy link
Contributor

ehuss commented 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.

@eggyal
Copy link
Contributor

eggyal commented Feb 27, 2023

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.

@lolbinarycat
Copy link
Contributor

related: #101353

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools
Projects
None yet
Development

No branches or pull requests

4 participants