You've already forked comprehensive-rust
mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-11-28 00:39:01 +02:00
Improve speaker notes and exercise in tuples and arrays section (#2909)
This commit is contained in:
@@ -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.
|
||||
|
||||
</details>
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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.
|
||||
|
||||
</details>
|
||||
|
||||
Reference in New Issue
Block a user