cognitive-engineering-lab / rust-book

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

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

Open varalgit opened 1 month ago

varalgit commented 1 month 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 6 days ago

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