1 00:00:05,770 --> 00:00:10,570 In this lecture, we're going to discuss data types and we're going to start off talking about what 2 00:00:10,570 --> 00:00:12,010 scalar types are. 3 00:00:12,040 --> 00:00:19,000 And scalar types represent a single value, and rust has four primary scalar types and they are integers, 4 00:00:19,000 --> 00:00:22,570 floating point booleans and characters. 5 00:00:23,140 --> 00:00:28,810 So I went ahead and did is I used cargo new to create a new directory called data types. 6 00:00:29,080 --> 00:00:36,280 And in here we're going to look at integers, integer, literals, floating points, booleans and characters. 7 00:00:37,060 --> 00:00:40,900 To start off, we're going to start off with integers and integers. 8 00:00:41,810 --> 00:00:48,830 B eight bit, 16 bit, 32 bit, 64 bit and 128 bit. 9 00:00:48,950 --> 00:00:51,170 So if we wanted to create. 10 00:00:52,230 --> 00:00:55,500 An eight bit integer, we can say let. 11 00:00:56,610 --> 00:01:06,270 X of type I eight, which is going to stand for integer that is eight bit equal. 12 00:01:06,270 --> 00:01:09,060 And then for this we'll just do ten. 13 00:01:10,240 --> 00:01:12,940 So now if we just went ahead and printed this out. 14 00:01:14,580 --> 00:01:18,650 We would get ten. 15 00:01:18,660 --> 00:01:23,520 So now we'll go down here, cargo build and cargo run. 16 00:01:23,850 --> 00:01:26,820 And as you can see, ten was printed out at the bottom. 17 00:01:28,480 --> 00:01:32,320 So now integers can also be signed or unsigned. 18 00:01:33,370 --> 00:01:40,030 And what that means is that if it's signed, it can be positive or negative. 19 00:01:40,330 --> 00:01:44,600 But if it's unsigned, that means it won't have a plus or minus minus. 20 00:01:44,620 --> 00:01:52,300 So only use unsigned integers if the value you want is for it to always be positive. 21 00:01:52,600 --> 00:01:54,040 So it works the same way. 22 00:01:54,040 --> 00:01:58,490 So we'll say let y and then we'll do you for unsigned eight. 23 00:01:58,510 --> 00:02:03,040 And right here you can see where we have all of our unsigned bits as well. 24 00:02:03,040 --> 00:02:07,090 So we have eight, 16, 32, 64 and 128. 25 00:02:07,090 --> 00:02:11,650 So we'll do eight again and then we'll just enter this one to also be ten. 26 00:02:12,760 --> 00:02:16,810 So this is creating an unsigned eight bit integer. 27 00:02:16,810 --> 00:02:21,070 So no matter what we did, it will always be positive. 28 00:02:22,240 --> 00:02:26,620 So the next thing that we can talk about are integer literals. 29 00:02:27,380 --> 00:02:35,150 So what we'll do here is we'll call create a variable called decimal and we're going to assign it zero 30 00:02:35,150 --> 00:02:37,430 to underscore five five. 31 00:02:37,850 --> 00:02:47,360 So this is going to create a variable called decimal and it's going to contain a value of 255 and decimal 32 00:02:47,360 --> 00:02:48,020 format. 33 00:02:48,020 --> 00:02:50,090 So we can also do this with hex. 34 00:02:50,240 --> 00:02:57,920 So if we do that, create a variable called hex and assign it a hex value that will also be. 35 00:02:59,890 --> 00:03:01,040 You 55. 36 00:03:01,130 --> 00:03:01,430 Correct. 37 00:03:01,430 --> 00:03:02,360 That typo? 38 00:03:02,600 --> 00:03:03,050 Nope. 39 00:03:03,800 --> 00:03:04,340 There we go. 40 00:03:05,920 --> 00:03:08,290 And likewise we can also do this with octal. 41 00:03:08,680 --> 00:03:12,790 So we'll do octal as well and it'll be zero. 42 00:03:13,390 --> 00:03:15,220 The big with the little circle. 43 00:03:15,250 --> 00:03:16,990 Circle with a little. 44 00:03:19,180 --> 00:03:27,250 So there we go and do 377, which will also be 255. 45 00:03:28,060 --> 00:03:30,460 And then finally we can do binary. 46 00:03:31,860 --> 00:03:38,790 And set it to zero be 1111 underscore 1111. 47 00:03:39,180 --> 00:03:41,560 And that will be 255. 48 00:03:41,580 --> 00:03:44,970 So now if we went ahead and printed all of these out. 49 00:03:50,000 --> 00:03:55,040 And then for sake of time, I'm going to copy and paste. 50 00:03:57,930 --> 00:03:58,800 And we will go. 51 00:04:00,060 --> 00:04:00,990 A simple 52 00:04:03,360 --> 00:04:14,280 hex, octal and binary cargo build, cargo run. 53 00:04:14,280 --> 00:04:19,320 And as you can see, we have 255 for all of those above. 54 00:04:20,650 --> 00:04:24,100 Another one that I can point out is a bite. 55 00:04:24,340 --> 00:04:29,080 So we can say let bite equal. 56 00:04:30,570 --> 00:04:31,680 Lowercase B. 57 00:04:32,730 --> 00:04:34,890 Apostrophe Big A. 58 00:04:36,750 --> 00:04:37,440 Close it off. 59 00:04:37,440 --> 00:04:44,790 And this is going to print out the ASCII value of what a Capital A is. 60 00:04:45,150 --> 00:04:56,450 And I believe it is going to be 64 or 65 and it is 65, as you can see right there. 61 00:04:56,460 --> 00:05:06,150 So now what we're seeing right here and this is just a little tidbit we we created a Y value, a Y variable, 62 00:05:06,150 --> 00:05:08,340 and we never used it. 63 00:05:08,340 --> 00:05:13,830 So if you want to silence these unused variable warnings, you can put an underscore in it. 64 00:05:14,400 --> 00:05:18,990 So now if we cargo build in card go run. 65 00:05:19,470 --> 00:05:27,390 It's a completely clean all the warnings are silenced but we could also comment it out with two forward 66 00:05:27,390 --> 00:05:34,890 slashes and it'll turn the text screen that's pretty universal and programming languages on how to comment 67 00:05:34,890 --> 00:05:35,880 in your code. 68 00:05:37,600 --> 00:05:40,150 So what we'll do is we'll comment all this out. 69 00:05:42,030 --> 00:05:45,450 And we'll go down and now we're going to look at floating point. 70 00:05:46,900 --> 00:05:52,930 So floats are going to work a lot like integers and the way you instantiate them. 71 00:05:53,320 --> 00:05:56,740 So what we'll do is we'll, we can do it this way. 72 00:05:56,740 --> 00:06:01,150 So we'll do X again equals 2.0. 73 00:06:01,390 --> 00:06:07,990 And what this is going to do is it's going to create an F 64, which stands for float 64 bits. 74 00:06:07,990 --> 00:06:14,470 And this is default because on modern CPUs. 75 00:06:17,350 --> 00:06:19,840 It's roughly the same speed as. 76 00:06:20,670 --> 00:06:22,050 Floating point 32. 77 00:06:22,050 --> 00:06:28,110 So if we wanted a floating point 32, we could do it this way. 78 00:06:29,420 --> 00:06:31,820 And it's the same same thing. 79 00:06:34,050 --> 00:06:39,390 But floating point 64 obviously is going to give you better precision. 80 00:06:40,170 --> 00:06:41,670 So that's why it's default. 81 00:06:46,470 --> 00:06:49,420 Next we will look at booleans. 82 00:06:49,440 --> 00:06:52,710 So boolean is just going to be true or false. 83 00:06:53,130 --> 00:06:55,650 So the way we can do that is let. 84 00:06:56,380 --> 00:06:58,480 Be for true, equal, true. 85 00:06:59,680 --> 00:07:05,590 And then let f boolean equal false. 86 00:07:05,770 --> 00:07:06,850 Either way it works. 87 00:07:06,850 --> 00:07:08,710 They both are the same thing. 88 00:07:08,710 --> 00:07:12,400 So the reason that this work works is because. 89 00:07:13,300 --> 00:07:22,810 Rust's compiler and runtime is smart enough to infer the type so it's going to know OC hey, the variable 90 00:07:22,840 --> 00:07:24,000 t is set to true. 91 00:07:24,010 --> 00:07:28,000 So I know that this variable is always going to be a boolean. 92 00:07:28,330 --> 00:07:36,670 So you can, you can implicitly tell it with that or you can let it figure it out on its own. 93 00:07:37,000 --> 00:07:39,610 Either one works kind of a preference. 94 00:07:41,530 --> 00:07:46,090 And lastly, the next one we're going to look at is a character type. 95 00:07:47,620 --> 00:07:48,940 So we'll do. 96 00:07:48,940 --> 00:07:49,540 Let's see. 97 00:07:49,540 --> 00:07:51,670 Four character equal. 98 00:07:52,600 --> 00:07:53,200 See? 99 00:07:54,460 --> 00:07:55,570 And it's the same thing. 100 00:07:55,570 --> 00:07:57,490 So if we wanted to print that out. 101 00:08:00,830 --> 00:08:02,660 And put that in there. 102 00:08:07,580 --> 00:08:08,660 Clear it off. 103 00:08:09,140 --> 00:08:09,890 Build it. 104 00:08:11,120 --> 00:08:11,750 Run it. 105 00:08:11,750 --> 00:08:16,460 And as you can see down here, we printed out this see character. 106 00:08:18,600 --> 00:08:20,430 Now there's a couple of arithmetic. 107 00:08:22,200 --> 00:08:29,340 Operators that we have you can do you have plus. 108 00:08:30,770 --> 00:08:31,760 Minus. 109 00:08:33,450 --> 00:08:37,440 Multiply divide and then remainder. 110 00:08:38,130 --> 00:08:44,310 So we'll create a couple of variables ten let be equal. 111 00:08:44,310 --> 00:08:51,090 Five And then what we'll do is just demonstrate. 112 00:08:53,770 --> 00:08:55,120 But the remainder is. 113 00:08:56,180 --> 00:08:56,770 Um. 114 00:08:57,910 --> 00:08:59,190 You know, ten plus five. 115 00:08:59,200 --> 00:09:00,630 We all know how that one's going to work. 116 00:09:00,640 --> 00:09:03,670 So just for sake of time, we'll do a remainder. 117 00:09:03,670 --> 00:09:09,880 So we're going to change B to four and we'll say let remainder equal to A. 118 00:09:11,750 --> 00:09:13,280 Remainder B. 119 00:09:14,510 --> 00:09:15,950 And then we'll print out. 120 00:09:18,220 --> 00:09:19,780 What the remainder is. 121 00:09:27,340 --> 00:09:31,930 And it should print out to. 122 00:09:35,410 --> 00:09:37,330 The arithmetic is super simple. 123 00:09:37,420 --> 00:09:40,480 The remainder one is one you might not have seen before. 124 00:09:41,680 --> 00:09:42,340 So yeah. 125 00:09:42,340 --> 00:09:44,740 So the sign is how you get. 126 00:09:45,580 --> 00:09:52,030 You remainder, and now you've learned the basic scalar types of rust. 127 00:09:52,210 --> 00:09:58,600 And the next lecture, we're going to talk about the compound types such as tuple arrays. 128 00:09:59,200 --> 00:10:00,250 So I'll see you then.