Book: Chapter 5: Add binding vs field mutability remarks & examples from the first addition

Created on 10 Sep 2017  路  1Comment  路  Source: rust-lang/book

Hi!

I understand that the chapter is frozen at this point. Probably this makes the suggestion useless, feel free to close straight away.

I liked the following section on structs from the first edition:

Rust does not support field mutability at the language level, so you cannot write something like this:

> struct Point {
>     mut x: i32, // This causes an error.
>     y: i32,
> }

Mutability is a property of the binding, not of the structure itself. If you鈥檙e used to field-level mutability, this may seem strange at first, but it significantly simplifies things. It even lets you make things mutable on a temporary basis:

> struct Point {
>     x: i32,
>     y: i32,
> }
> 
> fn main() {
>     let mut point = Point { x: 0, y: 0 };
> 
>     point.x = 5;
> 
>     let point = point; // `point` is now immutable.
> 
>     point.y = 6; // This causes an error.
> }

I think it could be useful to keep it in addition to the example added per issue #769 .

Best,
Andrei

PS Love the book, love the language, I am so looking forward to doing something meaningful with it!!

Enhancement Q for nostarch

Most helpful comment

Thank you for your kind words and this suggestion! I'm going to ask our editors if we can sneak in a tiny bit next time we're working on Chapter 5, we'll see what they say.

>All comments

Thank you for your kind words and this suggestion! I'm going to ask our editors if we can sneak in a tiny bit next time we're working on Chapter 5, we'll see what they say.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

carols10cents picture carols10cents  路  5Comments

istarus picture istarus  路  4Comments

rafaelalvessa picture rafaelalvessa  路  5Comments

dlukes picture dlukes  路  3Comments

binarycrusader picture binarycrusader  路  4Comments