1 00:00:00,180 --> 00:00:05,190 In this lecture, we are going to learn how to debug an application with macros. 2 00:00:05,490 --> 00:00:07,740 Macros are an advanced subject. 3 00:00:08,039 --> 00:00:12,900 Unfortunately, we won't be able to dive into how macros are created. 4 00:00:13,230 --> 00:00:16,530 There isn't enough time to review the code of a macro. 5 00:00:16,830 --> 00:00:20,760 So instead, this lecture will be a light introduction to them. 6 00:00:21,150 --> 00:00:24,300 Don't worry, it'll be enough to work with WebAssembly. 7 00:00:24,810 --> 00:00:26,310 So what are macros? 8 00:00:26,520 --> 00:00:32,220 First, we need to understand something called meta programming, which can be a trippy concept. 9 00:00:32,490 --> 00:00:34,920 It's when a program can write a program. 10 00:00:34,920 --> 00:00:39,420 Literally copying and pasting code is a classic developer move. 11 00:00:39,600 --> 00:00:40,440 We all do it. 12 00:00:40,800 --> 00:00:44,130 What if we could write our program that copies and paste code? 13 00:00:44,520 --> 00:00:50,520 I'm oversimplifying it, but that's met our programming in a nutshell, bringing it back to rust. 14 00:00:50,760 --> 00:00:54,390 A macro is a feature for writing code in our program. 15 00:00:54,750 --> 00:01:00,510 Russ will copy and paste code defined in the macro, which is a form of meta programming. 16 00:01:00,840 --> 00:01:04,290 It can be a difficult concept to wrap your head around. 17 00:01:04,590 --> 00:01:06,780 Writing a macro is very difficult. 18 00:01:07,050 --> 00:01:13,260 Luckily, it's not something we'll need to do as long as we know how to use them will be good to go. 19 00:01:14,230 --> 00:01:16,390 Using a macro is very easy. 20 00:01:16,660 --> 00:01:20,650 The syntax is similar to a function in our main function. 21 00:01:20,680 --> 00:01:24,880 We are storing the sum of two numbers in a variable called fill. 22 00:01:25,300 --> 00:01:28,000 Let's output the results onto the console. 23 00:01:28,330 --> 00:01:33,700 Russ defines a macro for this specific action below the few variable. 24 00:01:33,760 --> 00:01:39,920 Let's add a macro called Prince l'N in the letters L and are short for a line. 25 00:01:40,360 --> 00:01:46,720 Almost immediately, you'll notice a strong similarity between calling a macro and calling a function. 26 00:01:47,080 --> 00:01:51,040 We write the name of the macro, followed by a pair of parentheses. 27 00:01:51,370 --> 00:01:56,530 The biggest difference between a macro and function is the exclamation mark character. 28 00:01:56,920 --> 00:01:58,720 It'll always appear after the name. 29 00:01:59,080 --> 00:02:01,570 This is how you can identify macros. 30 00:02:02,080 --> 00:02:08,289 Another similar feature a macro share with functions, is arguments in between the parentheses. 31 00:02:08,470 --> 00:02:11,680 We can pass on a value to print the terminal. 32 00:02:12,010 --> 00:02:15,040 Let's try passing in a Hello World message. 33 00:02:15,610 --> 00:02:19,840 Next, let's run our program with the cargo run commands. 34 00:02:20,410 --> 00:02:22,960 The message gets output it as desired. 35 00:02:23,170 --> 00:02:26,140 However, we get a warning from the compiler. 36 00:02:26,470 --> 00:02:29,530 It's telling us the few variable is unused. 37 00:02:29,800 --> 00:02:34,270 Let's replace the string with the few variable in the prince macro. 38 00:02:34,930 --> 00:02:36,820 The compiler will throw an error. 39 00:02:37,210 --> 00:02:42,490 It's very strict with the types of values we can pass on to macros and functions. 40 00:02:42,880 --> 00:02:46,690 On the one hand, it's very convenient, but not in this case. 41 00:02:47,080 --> 00:02:52,450 There are scenarios where we are going to need to debug values other than strings. 42 00:02:52,750 --> 00:02:56,470 We might want to debug arrays, numbers or billions. 43 00:02:56,980 --> 00:03:00,580 Luckily, the print line macro supports formatting. 44 00:03:00,940 --> 00:03:03,250 We can inject values into a string. 45 00:03:03,580 --> 00:03:08,110 Let's replace the few variable with a string inside the string. 46 00:03:08,230 --> 00:03:15,550 We're going to add a pair of curly braces the Curly Braces Act as a placeholder before robust outputs 47 00:03:15,550 --> 00:03:16,270 the string. 48 00:03:16,450 --> 00:03:20,110 It'll replace the placeholder with the value of our choosing. 49 00:03:20,530 --> 00:03:24,850 We can add a value as a second argument to the print line macro. 50 00:03:27,070 --> 00:03:31,270 Let's try running the cargo run command in the terminal. 51 00:03:31,360 --> 00:03:33,670 The variables value gets displayed. 52 00:03:34,030 --> 00:03:34,630 Perfect. 53 00:03:34,900 --> 00:03:38,830 We're able to debug our values by inspecting them in the terminal. 54 00:03:39,160 --> 00:03:42,910 The print line macro is more flexible than you can imagine. 55 00:03:43,210 --> 00:03:46,990 For example, what if we want to output multiple values? 56 00:03:47,290 --> 00:03:50,790 The argument list is limitless inside the string. 57 00:03:50,800 --> 00:03:52,660 Let's add another placeholder. 58 00:03:53,320 --> 00:03:55,720 An error will be thrown by the compiler. 59 00:03:55,960 --> 00:03:59,290 It's unable to find a value for the second placeholder. 60 00:03:59,620 --> 00:04:03,250 Let's add a Boolean value as an argument to the macro. 61 00:04:03,880 --> 00:04:06,400 Lastly, let's compile our code. 62 00:04:06,970 --> 00:04:09,610 Russ was able to replace our values. 63 00:04:09,880 --> 00:04:13,870 The order of placeholders will correlate to the order of arguments. 64 00:04:14,230 --> 00:04:18,130 The first placeholder will be replaced with the second argument. 65 00:04:18,550 --> 00:04:22,360 The second placeholder will be replaced with the third argument. 66 00:04:22,750 --> 00:04:24,160 So on and so forth. 67 00:04:24,460 --> 00:04:27,880 In some cases, we may want to reuse a value. 68 00:04:28,600 --> 00:04:32,080 The print macro supports positional arguments. 69 00:04:32,350 --> 00:04:36,370 I'm going to remove the Boolean value after removing it. 70 00:04:36,520 --> 00:04:41,740 The compiler will throw the same error as before instead of adding a new value. 71 00:04:41,770 --> 00:04:46,780 We can point to a specific argument by adding a number inside the placeholder. 72 00:04:47,110 --> 00:04:50,980 The number is a position to a value in our argument list. 73 00:04:51,460 --> 00:04:53,380 Positions are zero based. 74 00:04:53,650 --> 00:04:57,340 Therefore, zero will point to our food variable. 75 00:04:57,670 --> 00:05:00,250 Let's head zeros to both placeholders. 76 00:05:00,880 --> 00:05:03,370 Next, let's compile our project. 77 00:05:03,940 --> 00:05:06,850 Both placeholders are replaced with the number. 78 00:05:07,240 --> 00:05:14,260 There's one less trick I want to show you and some examples you may see developers use a different placeholder. 79 00:05:14,770 --> 00:05:18,250 The syntax is slightly different than a regular placeholder. 80 00:05:18,610 --> 00:05:21,610 Let's reset the string to a regular placeholder. 81 00:05:21,970 --> 00:05:27,190 Inside the curly braces, let's add a colon and question mark character. 82 00:05:27,760 --> 00:05:31,150 This type of placeholder works for complex values. 83 00:05:31,570 --> 00:05:38,200 Russ gives developers the flexibility of manipulating a value before it gets output it onto the terminal 84 00:05:38,200 --> 00:05:39,820 for debugging purposes. 85 00:05:40,210 --> 00:05:44,710 If a developer decides to do that, we will need to use this placeholder. 86 00:05:45,040 --> 00:05:47,260 Let's try compiling the project. 87 00:05:47,860 --> 00:05:50,020 We get the same output as before. 88 00:05:50,350 --> 00:05:53,770 It's not apparent as to why we will use this placeholder. 89 00:05:54,100 --> 00:06:00,070 I promise you, when we start working with complex values, this placeholder will come in handy. 90 00:06:01,460 --> 00:06:03,590 We've talked a lot about macros. 91 00:06:03,810 --> 00:06:10,640 They're similar to functions, functions and macros are written the same with the exception of the exclamation 92 00:06:10,640 --> 00:06:12,380 mark symbol for macros. 93 00:06:12,680 --> 00:06:14,540 They both have a list of arguments. 94 00:06:14,780 --> 00:06:17,900 Conceptually, they can achieve similar goals. 95 00:06:18,230 --> 00:06:21,140 We can outsource logic into both features. 96 00:06:21,500 --> 00:06:23,510 So what are the differences? 97 00:06:24,080 --> 00:06:31,250 Macros support the following features periodic arguments, pretty syntax and meta programming. 98 00:06:31,640 --> 00:06:34,490 These topics are beyond the scope of this course. 99 00:06:34,820 --> 00:06:39,530 I want to emphasize you don't need to know how to write macros for this course. 100 00:06:39,860 --> 00:06:45,740 The macros shipped with rust will suffice for most cases as long as you know how to use them. 101 00:06:45,860 --> 00:06:47,720 Everything will be smooth sailing. 102 00:06:47,960 --> 00:06:50,780 Let's continue our journey in the next lecture.