diff --git a/src/tuples-and-arrays/arrays.md b/src/tuples-and-arrays/arrays.md index 82132c4b..7ed8b387 100644 --- a/src/tuples-and-arrays/arrays.md +++ b/src/tuples-and-arrays/arrays.md @@ -57,6 +57,17 @@ fn main() { - We can use literals to assign values to arrays. +- Arrays are not heap-allocated. They are regular values with a fixed size known + at compile time, meaning they go on the stack. This can be different from what + students expect if they come from a garbage-collected language, where arrays + may be heap allocated by default. + +- There is no way to remove elements from an array, nor add elements to an + array. The length of an array is fixed at compile-time, and so its length + cannot change at runtime. + +## Debug Printing + - The `println!` macro asks for the debug implementation with the `?` format parameter: `{}` gives the default output, `{:?}` gives the debug output. Types such as integers and strings implement the default output, but arrays only @@ -65,9 +76,4 @@ fn main() { - Adding `#`, eg `{a:#?}`, invokes a "pretty printing" format, which can be easier to read. -- Arrays are not heap-allocated. They are regular values with a fixed size known - at compile time, meaning they go on the stack. This can be different from what - students expect if they come from a garbage-collected language, where arrays - may be heap allocated by default. - diff --git a/src/tuples-and-arrays/exercise.rs b/src/tuples-and-arrays/exercise.rs index e9c7d665..8976808e 100644 --- a/src/tuples-and-arrays/exercise.rs +++ b/src/tuples-and-arrays/exercise.rs @@ -33,9 +33,17 @@ fn main() { [301, 302, 303], ]; - dbg!(matrix); + println!("Original:"); + for row in &matrix { + println!("{:?}", row); + } + let transposed = transpose(matrix); - dbg!(transposed); + + println!("\nTransposed:"); + for row in &transposed { + println!("{:?}", row); + } } // ANCHOR_END: main // ANCHOR_END: solution diff --git a/src/tuples-and-arrays/tuples.md b/src/tuples-and-arrays/tuples.md index e48a4aaf..9be14d03 100644 --- a/src/tuples-and-arrays/tuples.md +++ b/src/tuples-and-arrays/tuples.md @@ -26,4 +26,11 @@ fn main() { - The empty tuple `()` is referred to as the "unit type" and signifies absence of a return value, akin to `void` in other languages. +- Unlike arrays, tuples cannot be used in a `for` loop. This is because a `for` + loop requires all the elements to have the same type, which may not be the + case for a tuple. + +- There is no way to add or remove elements from a tuple. The number of elements + and their types are fixed at compile time and cannot be changed at runtime. +