1 00:00:06,600 --> 00:00:08,040 - In the last couple of sessions, 2 00:00:08,040 --> 00:00:10,140 we've seen how to declare an array, 3 00:00:10,140 --> 00:00:13,440 and we saw that an array is a fixed size collection, 4 00:00:13,440 --> 00:00:15,720 where each element is the same type. 5 00:00:15,720 --> 00:00:18,720 So what we're going to do now is look at tuples, 6 00:00:18,720 --> 00:00:19,890 kind of similar, 7 00:00:19,890 --> 00:00:22,410 but where each element can be different types. 8 00:00:22,410 --> 00:00:26,370 So a tuple is fixed size, heterogeneous collection. 9 00:00:26,370 --> 00:00:29,190 The elements can be of different types. 10 00:00:29,190 --> 00:00:32,250 Instead of using square brackets to create like we would 11 00:00:32,250 --> 00:00:35,760 for an array, we use parentheses to create a tuple. 12 00:00:35,760 --> 00:00:37,500 So this here is a tuple 13 00:00:37,500 --> 00:00:39,270 with three elements. 14 00:00:39,270 --> 00:00:40,740 Start at element zero. 15 00:00:40,740 --> 00:00:43,137 That's element zero, that's element one, 16 00:00:43,137 --> 00:00:44,730 and that's element two. 17 00:00:44,730 --> 00:00:46,920 Okay, so parentheses instead 18 00:00:46,920 --> 00:00:48,783 of square brackets for a tuple. 19 00:00:49,680 --> 00:00:51,880 If you want to, of course you can declare a tuple 20 00:00:51,880 --> 00:00:56,880 as a multiple tuple, in which case you can change the values 21 00:00:57,240 --> 00:00:59,130 but you can't change the types, 22 00:00:59,130 --> 00:01:01,680 and you can't change the size either. 23 00:01:01,680 --> 00:01:03,840 Okay? So if you replace one of the elements, 24 00:01:03,840 --> 00:01:06,210 you must replace it with the correct type 25 00:01:06,210 --> 00:01:09,090 and you can't resize it to be bigger. 26 00:01:09,090 --> 00:01:11,460 So here's an example of a tuple. 27 00:01:11,460 --> 00:01:13,020 T one is a tuple. 28 00:01:13,020 --> 00:01:15,780 The parentheses indicate that. 29 00:01:15,780 --> 00:01:18,270 The first element is an integer. 30 00:01:18,270 --> 00:01:20,940 The second element is a string of some kind. 31 00:01:20,940 --> 00:01:23,140 The third element is a floating point value. 32 00:01:23,984 --> 00:01:27,237 T1 is immutable, T2 is mutable. 33 00:01:27,237 --> 00:01:30,180 And T2 contains, well initially the same values 34 00:01:30,180 --> 00:01:32,400 but they could change later on. 35 00:01:32,400 --> 00:01:35,190 The main use for tuples really is to declare 36 00:01:35,190 --> 00:01:37,770 or to return more than one value from a function. 37 00:01:37,770 --> 00:01:41,160 So what you could do, let's say you want to return a bunch 38 00:01:41,160 --> 00:01:43,410 of values from a function, you could put them 39 00:01:43,410 --> 00:01:46,320 into a structure, but that's a bit of extra work. 40 00:01:46,320 --> 00:01:48,090 You'd have to declare the structure type, 41 00:01:48,090 --> 00:01:50,880 and maybe you can't be bothered to do that. 42 00:01:50,880 --> 00:01:54,323 So instead, you can just throw the values into a tuple. 43 00:01:54,323 --> 00:01:56,010 A tuple is like a bag, 44 00:01:56,010 --> 00:01:58,530 and then return that tuple back to the caller. 45 00:01:58,530 --> 00:02:00,843 That'd be the main use of tuples in practice. 46 00:02:01,800 --> 00:02:04,590 So if you want to access an element in the tuple, 47 00:02:04,590 --> 00:02:07,800 let's say you've got a tuple object called tup, 48 00:02:07,800 --> 00:02:10,440 you've gotta use a dot and an index syntax, 49 00:02:10,440 --> 00:02:13,620 you can't use square brackets like you would with an array. 50 00:02:13,620 --> 00:02:16,380 You can't say tuple square bracket zero. 51 00:02:16,380 --> 00:02:19,050 You've gotta say tuple dot, and then the index 52 00:02:19,050 --> 00:02:19,883 of the element you're after, 53 00:02:19,883 --> 00:02:21,303 like tuple.zero. 54 00:02:22,860 --> 00:02:26,850 So rust does bounds-checking at compile time, right? 55 00:02:26,850 --> 00:02:27,960 Compile time. 56 00:02:27,960 --> 00:02:30,540 It'll check that you said tuple.index is the 57 00:02:30,540 --> 00:02:33,630 index between zero and length minus one. 58 00:02:33,630 --> 00:02:36,000 And if it isn't, then you get a compile it error. 59 00:02:36,000 --> 00:02:38,340 Okay? So obviously you have to be careful. 60 00:02:38,340 --> 00:02:40,140 Things that you can't do with a tuple, 61 00:02:40,140 --> 00:02:42,210 its not as flexible as an array. 62 00:02:42,210 --> 00:02:44,130 Okay? So with an array 63 00:02:44,130 --> 00:02:46,710 you can get its length, but you can't, with a tuple, 64 00:02:46,710 --> 00:02:49,170 you can't say tuple object.length. 65 00:02:49,170 --> 00:02:50,640 You can't do that. 66 00:02:50,640 --> 00:02:52,020 And also you can't iterate 67 00:02:52,020 --> 00:02:53,910 through the elements in the tuple, okay? 68 00:02:53,910 --> 00:02:56,640 Because the main reason is the elements could be 69 00:02:56,640 --> 00:02:57,473 different data types. 70 00:02:57,473 --> 00:02:58,920 They probably will be. 71 00:02:58,920 --> 00:03:01,110 So if the first element's an integer, 72 00:03:01,110 --> 00:03:02,280 and the second is a strain, 73 00:03:02,280 --> 00:03:04,800 and the third is a double, how does this construct work? 74 00:03:04,800 --> 00:03:06,240 What type would this element be? 75 00:03:06,240 --> 00:03:07,530 It just doesn't work. 76 00:03:07,530 --> 00:03:10,830 So you can only access elements one by one 77 00:03:10,830 --> 00:03:13,020 using an index number with a dot. 78 00:03:13,020 --> 00:03:15,720 So it's much more kind of fixed, 79 00:03:15,720 --> 00:03:18,330 less dynamic if you like, than an array. 80 00:03:18,330 --> 00:03:20,190 It allows you to have multiple data types. 81 00:03:20,190 --> 00:03:21,423 That's the key point. 82 00:03:22,680 --> 00:03:25,650 So, well here you can have an empty tuple. 83 00:03:25,650 --> 00:03:29,400 The empty parenthesis, it's a tuple with no elements, okay? 84 00:03:29,400 --> 00:03:31,140 So it would be useful 85 00:03:31,140 --> 00:03:34,290 for a function that doesn't want to return anything. 86 00:03:34,290 --> 00:03:36,300 You could return an empty tuple. 87 00:03:36,300 --> 00:03:39,450 That would be the kind of use case for an empty tuple, 88 00:03:39,450 --> 00:03:41,943 a return value that's empty from a function. 89 00:03:44,250 --> 00:03:47,271 So if you want to declare a tuple variable 90 00:03:47,271 --> 00:03:50,910 but you're not yet ready to give it the values, 91 00:03:50,910 --> 00:03:53,280 then what you can do is you can, you can declare a tuple 92 00:03:53,280 --> 00:03:56,289 and then use the parentheses to specify the type 93 00:03:56,289 --> 00:03:59,640 of element zero, the type of element one, 94 00:03:59,640 --> 00:04:02,283 and the type of element two, and so on and so on. 95 00:04:03,390 --> 00:04:07,920 So when you're then ready you can assign your tuple object 96 00:04:07,920 --> 00:04:09,780 with values of your choice. 97 00:04:09,780 --> 00:04:11,160 Okay? Given that the value 98 00:04:11,160 --> 00:04:13,773 of the correct type for each position. 99 00:04:14,880 --> 00:04:16,230 So here's an example. 100 00:04:16,230 --> 00:04:18,000 I've got a tuple T4,. 101 00:04:18,000 --> 00:04:21,540 it's a tuple by virtue of the parentheses, 102 00:04:21,540 --> 00:04:24,270 where element zero, I haven't given it the values yet, 103 00:04:24,270 --> 00:04:27,000 but element zero will be a 32 bit float. 104 00:04:27,000 --> 00:04:31,170 Element one will be a Boolean, and element two will be 105 00:04:31,170 --> 00:04:33,030 a 64 bit float. 106 00:04:33,030 --> 00:04:36,930 So later on when I'm ready, I can assign it a 32 107 00:04:36,930 --> 00:04:39,390 bit integer, my age. 108 00:04:39,390 --> 00:04:41,400 I can assign it the Boolean value 109 00:04:41,400 --> 00:04:43,200 is this person Welsh? 110 00:04:43,200 --> 00:04:45,240 And I can assign, 'cause that's important. 111 00:04:45,240 --> 00:04:48,240 And then I can assign a floating point value my height 112 00:04:48,240 --> 00:04:50,940 okay? As long as the elements of the same type, 113 00:04:50,940 --> 00:04:52,350 then that's okay. 114 00:04:52,350 --> 00:04:55,740 So specify the types upfront, give the values later 115 00:04:55,740 --> 00:04:58,470 when you're ready, right? 116 00:04:58,470 --> 00:05:03,390 So rust tuples implement debug just like arrays. 117 00:05:03,390 --> 00:05:07,260 In other words, the tuple data type has a format function 118 00:05:07,260 --> 00:05:10,245 which basically displays the elements of the tuple 119 00:05:10,245 --> 00:05:12,832 in a user-friendly format. 120 00:05:12,832 --> 00:05:16,350 Okay? So you can use the debug formatter like this 121 00:05:16,350 --> 00:05:20,490 with the tuple curly bracket, colon, question mark. 122 00:05:20,490 --> 00:05:22,830 And then here's my tuple object. 123 00:05:22,830 --> 00:05:25,800 So effectively what will happen is it'll call the format 124 00:05:25,800 --> 00:05:27,870 function on the tuple. 125 00:05:27,870 --> 00:05:31,680 And that's pre-written, that's already defined in rust. 126 00:05:31,680 --> 00:05:34,350 The format function will output all the elements 127 00:05:34,350 --> 00:05:36,480 that the tuple contains. 128 00:05:36,480 --> 00:05:38,850 So an example, let's go to the demo project. 129 00:05:38,850 --> 00:05:40,980 We'll have a look at demo tuple as a function 130 00:05:40,980 --> 00:05:43,440 and then we'll run the project like usual. 131 00:05:43,440 --> 00:05:45,390 Let's have a look at the code. 132 00:05:45,390 --> 00:05:48,099 So here's my demo project, compound collections, 133 00:05:48,099 --> 00:05:50,790 in my source main. 134 00:05:50,790 --> 00:05:54,690 I'm going to un comment the call to demo tuples. 135 00:05:54,690 --> 00:05:57,780 Let's un comment that and let's take a look at it, 136 00:05:57,780 --> 00:05:59,430 down here. 137 00:05:59,430 --> 00:06:02,190 And there it is in all its glory. 138 00:06:02,190 --> 00:06:04,473 We'll run it so we can discuss it. 139 00:06:07,470 --> 00:06:09,480 All right, here we go. 140 00:06:09,480 --> 00:06:10,440 Using tuples. 141 00:06:10,440 --> 00:06:13,230 A tuple with those values. 142 00:06:13,230 --> 00:06:15,570 That's an immutable tuple. 143 00:06:15,570 --> 00:06:18,041 And I can access the elements one by one. 144 00:06:18,041 --> 00:06:23,041 Tuple object.zero, tuple object.one, tuple object.two. 145 00:06:24,000 --> 00:06:28,413 If I give it a value, an index out of range compiler error. 146 00:06:29,280 --> 00:06:33,120 Okay? So it outputs the tuple elements like so. 147 00:06:33,120 --> 00:06:36,060 Here's a mutable tuple, which I then change one 148 00:06:36,060 --> 00:06:38,550 of the values of, I change element zero 149 00:06:38,550 --> 00:06:41,370 and then I output the elements of tuple two. 150 00:06:41,370 --> 00:06:43,800 And here are the elements in tuple two. 151 00:06:43,800 --> 00:06:45,510 That's marvelous. 152 00:06:45,510 --> 00:06:48,900 And here I have an empty tuple, which I output. 153 00:06:48,900 --> 00:06:50,310 And here's my empty tuple. 154 00:06:50,310 --> 00:06:52,080 It has no elements. 155 00:06:52,080 --> 00:06:53,670 And then finally, a tuple. 156 00:06:53,670 --> 00:06:57,240 I specify the types here, I give the values later. 157 00:06:57,240 --> 00:06:58,530 And then in this example, 158 00:06:58,530 --> 00:07:01,500 I'm using a debug formatter to output the whole tuple all 159 00:07:01,500 --> 00:07:02,520 at once. 160 00:07:02,520 --> 00:07:04,800 And that's displayed here. 161 00:07:04,800 --> 00:07:06,150 And then if I want to, 162 00:07:06,150 --> 00:07:07,800 I can then access the elements individually 163 00:07:07,800 --> 00:07:10,530 one by one using the individual indexes. 164 00:07:10,530 --> 00:07:13,947 Tuple four, element zero, tuple four element one, 165 00:07:13,947 --> 00:07:16,170 and tuple four element two. 166 00:07:16,170 --> 00:07:17,003 Fantastic.