New usize explanation 008, etc.

This commit is contained in:
Dave Gauer 2021-06-14 11:26:04 -04:00
parent 989648d2cd
commit de322fab46
2 changed files with 22 additions and 9 deletions

View file

@ -1,7 +1,7 @@
// //
// Quiz time! Let's see if you can fix this whole program. // Quiz time! Let's see if you can fix this whole program.
// //
// This is meant to be challenging. // You'll have to think about this one a bit.
// //
// Let the compiler tell you what's wrong. // Let the compiler tell you what's wrong.
// //
@ -13,14 +13,23 @@ pub fn main() void {
// What is this nonsense? :-) // What is this nonsense? :-)
const letters = "YZhifg"; const letters = "YZhifg";
// Note: usize is an unsigned integer type used for...sizes.
// The exact size of usize depends on the target CPU
// architecture. We could have used a u8 here, but usize is
// the idiomatic type to use for array indexing.
//
// There IS a problem on this line, but 'usize' isn't it.
const x: usize = 1; const x: usize = 1;
// This is something you haven't seen before: declaring an array // Note: When you want to declare memory (an array in this
// without putting anything in it. There is no error here: // case) without putting anything in it, you can set it to
// 'undefined'. There is no problem on this line.
var lang: [3]u8 = undefined; var lang: [3]u8 = undefined;
// The following lines attempt to put 'Z', 'i', and 'g' into the // The following lines attempt to put 'Z', 'i', and 'g' into the
// 'lang' array we just created. // 'lang' array we just created by indexing the array
// 'letters' with the variable 'x'. As you can see above, x=1
// to begin with.
lang[0] = letters[x]; lang[0] = letters[x];
x = 3; x = 3;

View file

@ -1,12 +1,16 @@
16c16 22c22
< const x: u8 = 1; < const x: usize = 1;
--- ---
> var x: u8 = 1; > var x: usize = 1;
27c27 26c26
< // 'undefined'. There is no problem on this line.
---
> // 'undefined'. There is no error here.
36c36
< lang[???] = letters[x]; < lang[???] = letters[x];
--- ---
> lang[1] = letters[x]; > lang[1] = letters[x];
29,30c29,30 38,39c38,39
< x = ???; < x = ???;
< lang[2] = letters[???]; < lang[2] = letters[???];
--- ---