Book: Slices: "length" inner data vs `[start..end]` syntax is confusing

Created on 26 May 2016  路  8Comments  路  Source: rust-lang/book

In the "Slices" chapter, there are several sentences like this: "_It works the exact same way as string slices do, with a reference to the first element, and a length._". It's misleading because it induces that a slice's arguments are beginning and length, whereas they are beginning and end.

Bug

Most helpful comment

I understand that, but it's an implementation detail. From the POV of the rust user a slice is just its indexing syntax. IMHO.

All 8 comments

I might be misunderstanding you, but slices are definitely a pointer and a length, not a beginning and an end.

Aren't they written e.g s[1..3] for a 2 elements slice starting at the second element ?

Ah, I see the confusion here! So yes, you're right that that indexing syntax is beginning, end, but _the slice itself_ is a pointer and a length. Like:

let s = "slices";
let slice = &s[1..3];

Here, slice is a tuple: a pointer to l, and the number 2. That's the way it's implemented in memory.

I understand that, but it's an implementation detail. From the POV of the rust user a slice is just its indexing syntax. IMHO.

It's important to understand how it's represented though, and that slicing syntax is only one way to create a slice.

The answer is probably then to clarify the wording to better distinguish between the notation for ranges when creating a slice, and the in-memory representation of a slice.

Yes, agreed.

Precisely.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rpjohnst picture rpjohnst  路  4Comments

elahn picture elahn  路  3Comments

dlukes picture dlukes  路  3Comments

drandreaskrueger picture drandreaskrueger  路  4Comments

rafaelalvessa picture rafaelalvessa  路  5Comments