Book: When are Array elements bound via let instead of being copied

Created on 18 Sep 2016  路  3Comments  路  Source: rust-lang/book

In 3.2. Data Types:

In this example, the first variable will bind to 1 at index [0] in the array, and second will bind to 2 at index [1] in the array. Note that these values are copied out of the array and into first and second when the let statement is called. That means if the array changes after the let statements, these bindings will not, and the two variables should retain their values.

Sorry for the click-bait title of this issue, but it's a logical question after reading the paragraph above. Stating the _values are copied out of the array_ and _the two variables should retain their values_, is confusing.

As an aside, the phrasing treats first and second more like variables than variable bindings. Although, I don't see how to phrase this succinctly without doing that.

Bug

Most helpful comment

I think I'm going to dance around this for now as follows:

In this example, the binding named first will get the value 1, since that
is the value at index [0] in the array. The binding named second will get
the value 2 from index [1] in the array.

All 3 comments

Yeah, this is confusing since we haven't gotten to the Copy trait yet :( This behavior happens because the value stored in the array is a simple integer, basically.

Now that I reread this, the word "should" confuses things, definitely-- does that mean that sometimes the variables won't retain their values, but if they don't, there's a problem?

As an aside, the phrasing treats first and second more like variables than variable bindings.

I'm not sure how to convey this properly either, but Rust doesn't have variables, it _only_ has variable bindings. I think the distinction you're drawing here, caused by the confusing text, is that "variable bindings" is when the variable binding contains a borrowed reference to data that some other binding owns, and that "variables" are when a variable binding owns the data? It _is_ useful to distinguish those two scenarios, but it's more accurate to think of the two as "borrowing" vs "owning" rather than "variable binding" vs "variable", again, since "variable" doesn't technically exist in Rust.

This is tough and picky, and it's hard to hand-wave over complicated things at the beginning of introducing a complex topic without being inaccurate/confusing :(

Also I didn't find the title click-baity; click-baity would be "variable bindings are absolutely horrible" ;)

Let me think on this a bit and I'll propose a rewording. Thank you for the report! 鉂わ笍

I think I'm going to dance around this for now as follows:

In this example, the binding named first will get the value 1, since that
is the value at index [0] in the array. The binding named second will get
the value 2 from index [1] in the array.

Nice!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

BedfordWest picture BedfordWest  路  4Comments

ctsa picture ctsa  路  3Comments

mikebenfield picture mikebenfield  路  3Comments

binarycrusader picture binarycrusader  路  4Comments

icanrealizeum picture icanrealizeum  路  3Comments