cognitive-engineering-lab / rust-book

The Rust Programming Language: Experimental Edition
https://rust-book.cs.brown.edu
Other
636 stars 100 forks source link

In chapter 3.2, give examples of mutable arrays and tuples #190

Closed varalgit closed 4 months ago

varalgit commented 6 months ago

URL to the section(s) of the book with this problem:

https://rust-book.cs.brown.edu/ch03-02-data-types.html

In this chapter, only immutable examples of tuples and arrays are given, I suggest to also give examples of mutable tuples and arrays. Also, when a tuple or array is copied, a deep copy is made (unlike in Python for example), it would be better if that would be made clear.

For instance:

let mut a = (1, "2", 3, 4, 5);
let mut b = a;
a.0 = 3;
b.1 = "3";
println!("The value of a is: {:?}", a);
println!("The value of b is: {:?}", b);
let mut a = [1, 2, 3, 4, 5];
let mut b = a;
a[0] = 3;
b[1] = 3;
println!("The value of a is: {:?}", a);
println!("The value of b is: {:?}", b);
willcrichton commented 5 months ago

Yeah that's a nice idea. I'll add an example.