From 9cd5d258217db31ce01cefb25087a01399801d4c Mon Sep 17 00:00:00 2001 From: Robin Stringer Date: Mon, 9 Jan 2023 14:18:37 +0000 Subject: [PATCH] Adds speaker notes to Compound Types section Speaker notes for 6. Basic Syntax, 6.2 Compound Types section. Briefly explains arrays and tuples properties. Adds option for instructor to check for out of bounds errors using assert!(). --- src/basic-syntax/compound-types.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/basic-syntax/compound-types.md b/src/basic-syntax/compound-types.md index bf534596..7ad8e25c 100644 --- a/src/basic-syntax/compound-types.md +++ b/src/basic-syntax/compound-types.md @@ -24,3 +24,33 @@ fn main() { println!("2nd index: {}", t.1); } ``` + +
+ +Key points: + +Arrays: + +*Arrays have elements of the same type, T, and length, N, which is fixed. + +*We can use literals to assign values to arrays. + +*Demonstrate out of bounds errors by setting n to different values + e.g. a[n+15] = 11 // index out of bounds error since len is 10 + +*Efficient way to check n is in bounds: + assert!(n+20 < a.len()); // panics + +*In the main function, the print statement contains the debug implementation {a :?}. + +*Adding #, eg {a:#?}, invokes a "pretty printing" format, which can be easier to read. + +Tuples: + +*Like arrays, tuples have a fixed length. + +*Tuples group together values of different types into a compound type. + +*Fields that can be accessed by the period and the index of the value, e.g. t.0, t.1. + +