1 00:00:00,000 --> 00:00:08,132 [No Audio] 2 00:00:08,133 --> 00:00:10,800 This tutorial is about functions, which is one 3 00:00:10,801 --> 00:00:12,990 of the fundamental and basic concepts in 4 00:00:12,991 --> 00:00:15,630 programming. In addition to functions, we will 5 00:00:15,631 --> 00:00:17,910 also be covering the basics of how to take 6 00:00:17,942 --> 00:00:20,366 user input in Rust, so let's start. 7 00:00:21,400 --> 00:00:24,150 We will start by looking at the details of functions. 8 00:00:24,690 --> 00:00:27,060 A function in a programming language is a 9 00:00:27,061 --> 00:00:29,730 program fragment or segment that is designed 10 00:00:29,731 --> 00:00:33,060 to perform a defined or specific task. For 11 00:00:33,061 --> 00:00:35,820 example, a function may be written that finds 12 00:00:35,821 --> 00:00:37,950 the average of the three supplied numbers. 13 00:00:38,460 --> 00:00:40,980 Once written, this function may be used many 14 00:00:40,981 --> 00:00:43,830 times without having to rewrite it over and 15 00:00:43,860 --> 00:00:46,530 over again. Functions are a great way for 16 00:00:46,531 --> 00:00:48,720 organizing your code, and give a modular 17 00:00:48,721 --> 00:00:50,790 structure and look to your code, and are 18 00:00:50,791 --> 00:00:53,070 frequently being used almost across all 19 00:00:53,071 --> 00:00:55,980 programming languages. Two basic 20 00:00:55,981 --> 00:00:58,710 terminologies with regards to functions are 21 00:00:58,890 --> 00:01:00,960 known as function call and function 22 00:01:00,961 --> 00:01:03,510 definition. Using the function inside the 23 00:01:03,511 --> 00:01:05,640 main program to perform certain task is 24 00:01:05,641 --> 00:01:08,040 called a function call, and defining the 25 00:01:08,041 --> 00:01:10,050 function or the code segment inside a 26 00:01:10,051 --> 00:01:12,240 function is called function definition. 27 00:01:12,810 --> 00:01:14,670 Please note that the functions are written 28 00:01:14,700 --> 00:01:18,030 outside the main function. The main function 29 00:01:18,031 --> 00:01:20,190 as the name suggests, is also a function 30 00:01:20,191 --> 00:01:22,410 which is like an entry point into the code, 31 00:01:23,184 --> 00:01:25,866 this function will always get a chance to execute. 32 00:01:26,900 --> 00:01:28,890 A function will take zero or many 33 00:01:28,891 --> 00:01:31,470 inputs and will have zero or many outputs, 34 00:01:31,860 --> 00:01:34,380 let us define a very basic function with no 35 00:01:34,381 --> 00:01:37,710 inputs and outputs. As pointed out, the 36 00:01:37,711 --> 00:01:39,450 functions are written outside the main 37 00:01:39,451 --> 00:01:41,820 function. So I will go outside the body of 38 00:01:41,821 --> 00:01:45,330 the main function. This curly braket marks 39 00:01:45,331 --> 00:01:47,940 the end of the main function. After this 40 00:01:47,941 --> 00:01:50,520 curly brackets, I will define a simple 41 00:01:50,521 --> 00:01:53,130 function, functions are declared using the 42 00:01:53,131 --> 00:01:55,860 reserved word of fn followed by the name of 43 00:01:55,861 --> 00:01:58,260 the function. I will define a function with 44 00:01:58,261 --> 00:02:01,440 the name of basic_fn, after the 45 00:02:01,441 --> 00:02:03,570 name of the function, we need to put smooth 46 00:02:03,571 --> 00:02:06,420 brackets, if there are any inputs to the 47 00:02:06,421 --> 00:02:08,669 function, they will be provided inside these 48 00:02:08,670 --> 00:02:11,790 brackets. Next, we will need to put two curly 49 00:02:11,791 --> 00:02:13,650 brackets which indicates the starting and 50 00:02:13,651 --> 00:02:16,590 ending part of the function. All the code 51 00:02:16,591 --> 00:02:18,780 that is between these two curly brackets are 52 00:02:18,781 --> 00:02:21,630 called the function body. Let us assume that 53 00:02:21,631 --> 00:02:23,640 this function is going to print some message 54 00:02:23,641 --> 00:02:26,610 to the user. Let me add a simple print 55 00:02:26,611 --> 00:02:28,050 statement for this purpose. 56 00:02:28,051 --> 00:02:33,060 [No Audio] 57 00:02:33,061 --> 00:02:34,410 Let us execute now. 58 00:02:34,411 --> 00:02:39,750 [No Audio] 59 00:02:39,751 --> 00:02:42,450 You may note that we do not have any output 60 00:02:42,600 --> 00:02:44,820 this is because by default, the compiler will 61 00:02:44,821 --> 00:02:47,490 only run the main function, since the 62 00:02:47,520 --> 00:02:50,100 function basic_fn is outside the 63 00:02:50,101 --> 00:02:52,590 body of the main function, therefore, it is 64 00:02:52,591 --> 00:02:55,650 not going to be executed. To make sure that 65 00:02:55,651 --> 00:02:57,780 it is being executed we will need to call 66 00:02:57,781 --> 00:02:59,760 this function in the main function by writing 67 00:02:59,761 --> 00:03:01,020 the name of this function. 68 00:03:01,021 --> 00:03:05,933 [No Audio] 69 00:03:05,934 --> 00:03:09,630 If I cargo run this now, you may note that 70 00:03:09,631 --> 00:03:13,170 the function executed in this case now, and we 71 00:03:13,200 --> 00:03:15,480 are therefore having the corresponding print 72 00:03:15,481 --> 00:03:18,450 message appearing on the terminal. This means 73 00:03:18,480 --> 00:03:20,610 that the function will only get a chance to 74 00:03:20,611 --> 00:03:22,860 execute when we make a call to the function. 75 00:03:24,150 --> 00:03:26,370 We may call the function many times, and each 76 00:03:26,371 --> 00:03:28,950 time we call it, we will have this output on 77 00:03:28,951 --> 00:03:31,710 the terminal. We do not need to rewrite the 78 00:03:31,711 --> 00:03:34,380 whole code over and over again. We will just 79 00:03:34,381 --> 00:03:36,330 refer to the code inside the function by 80 00:03:36,331 --> 00:03:38,333 calling it as many times as we like. 81 00:03:39,100 --> 00:03:41,730 Let us now learn functions having some inputs. 82 00:03:42,600 --> 00:03:44,490 Outside the main function, I will define 83 00:03:44,491 --> 00:03:48,500 another function with the name of, function_with_inputs. 84 00:03:48,501 --> 00:03:53,333 [No Audio] 85 00:03:53,334 --> 00:03:55,800 Inside these smooth brackets, I will declare some 86 00:03:55,801 --> 00:03:58,950 variables, that I want this function to accept 87 00:03:58,951 --> 00:04:01,650 as inputs and the first one will be a string 88 00:04:01,651 --> 00:04:05,070 reference variable. I will first write its 89 00:04:05,071 --> 00:04:07,200 name, and then I will mention its type. 90 00:04:07,201 --> 00:04:09,666 [No Audio] 91 00:04:09,667 --> 00:04:12,870 I will add one more input variable the name of the 92 00:04:12,871 --> 00:04:16,666 variable will be salary, and the type will be an i32. 93 00:04:17,366 --> 00:04:18,570 I want this function to 94 00:04:18,571 --> 00:04:20,579 display the two inputs, so I will write a 95 00:04:20,580 --> 00:04:21,766 print statement. 96 00:04:21,767 --> 00:04:28,980 [No Audio] 97 00:04:28,981 --> 00:04:30,780 Let us call this function from the main now 98 00:04:30,781 --> 00:04:33,540 with the inputs of Nouman and 40,000. 99 00:04:33,541 --> 00:04:39,690 [No Audio] 100 00:04:39,691 --> 00:04:42,240 In this case now, the first argument is the 101 00:04:42,241 --> 00:04:45,100 string and the second argument is an i32. 102 00:04:45,800 --> 00:04:47,640 If we provide different inputs to the 103 00:04:47,641 --> 00:04:49,440 function, the Rust compiler will complain. 104 00:04:49,441 --> 00:04:52,770 Because the function input needs to be 105 00:04:52,771 --> 00:04:55,440 in the same order and of the same type, as 106 00:04:55,441 --> 00:04:57,400 defined in the function definition. 107 00:04:57,468 --> 00:04:59,166 Let us execute this. 108 00:04:59,167 --> 00:05:03,033 [No Audio] 109 00:05:03,034 --> 00:05:04,770 We can confirm that the two 110 00:05:04,771 --> 00:05:08,490 inputs are being displayed. We can also call 111 00:05:08,491 --> 00:05:10,590 the same function by providing inputs in the 112 00:05:10,591 --> 00:05:12,690 form of variables instead of some fixed 113 00:05:12,691 --> 00:05:15,420 values, let us declare a couple of variables 114 00:05:15,450 --> 00:05:18,210 of type string and integer, the string 115 00:05:18,211 --> 00:05:21,090 variable will contain the string of Kamran 116 00:05:21,120 --> 00:05:24,066 and the integer variable will have a value of 50,000. 117 00:05:26,066 --> 00:05:28,050 I will call the function now with 118 00:05:28,051 --> 00:05:29,833 these two variables as inputs. 119 00:05:29,834 --> 00:05:34,400 [No Audio] 120 00:05:34,401 --> 00:05:35,900 Let us execute now. 121 00:05:35,901 --> 00:05:39,500 [No Audio] 122 00:05:39,501 --> 00:05:41,940 Yes as expected the two variables are 123 00:05:41,941 --> 00:05:43,920 being passed to the function and appropriate 124 00:05:43,921 --> 00:05:46,700 printing function has been successfully executed. 125 00:05:47,747 --> 00:05:50,001 Functions can have inputs as well 126 00:05:50,002 --> 00:05:52,230 as outputs, let us learn functions with 127 00:05:52,231 --> 00:05:55,170 outputs. I will write a simple function which 128 00:05:55,171 --> 00:05:57,270 will compute the multiplication result of the 129 00:05:57,271 --> 00:05:59,820 two input numbers and will return the result 130 00:05:59,821 --> 00:06:03,510 of the multiplication. I will name this 131 00:06:03,511 --> 00:06:08,166 function as function_with_inputs_outputs. 132 00:06:09,533 --> 00:06:11,300 There will be two inputs 133 00:06:11,301 --> 00:06:14,466 called num1 and num2 with type of i32. 134 00:06:14,467 --> 00:06:19,100 [No Audio] 135 00:06:19,101 --> 00:06:20,070 The outputs from the 136 00:06:20,071 --> 00:06:22,680 function are specified by a dash followed by 137 00:06:22,710 --> 00:06:24,900 a greater than sign and then the type of the 138 00:06:24,901 --> 00:06:28,230 output, which in this case is i32. Inside the 139 00:06:28,231 --> 00:06:31,166 curly brackets, I will write num1 times num2, 140 00:06:31,333 --> 00:06:34,033 which will compute the multiplication of the two numbers. 141 00:06:34,833 --> 00:06:37,710 the output value from the function is specified by some 142 00:06:37,711 --> 00:06:40,140 expression they do not have any semicolon 143 00:06:40,170 --> 00:06:43,320 afterwards, when the Rust sees such a 144 00:06:43,321 --> 00:06:45,870 statement inside the body of the function, it 145 00:06:45,871 --> 00:06:48,180 assumes that it is the respective output from 146 00:06:48,181 --> 00:06:50,700 the function, when there is one output from 147 00:06:50,701 --> 00:06:52,950 the function then there should be exactly one 148 00:06:52,951 --> 00:06:55,733 such statement where there is no semicolon after it. 149 00:06:56,533 --> 00:06:58,620 If we add another statement after 150 00:06:58,621 --> 00:07:01,066 the multiplication such as num1 + num2, 151 00:07:02,200 --> 00:07:03,930 then in this case, the Rust compiler 152 00:07:03,931 --> 00:07:06,150 will complain and it will rightfully complain 153 00:07:06,151 --> 00:07:08,580 because, there will be a confusion regarding 154 00:07:08,581 --> 00:07:11,580 which value should be the actual output 155 00:07:11,581 --> 00:07:14,190 from the function. Let us remove the 156 00:07:14,191 --> 00:07:16,566 statement of num1 + num2 for now. 157 00:07:18,633 --> 00:07:20,760 Also note that the output value from the 158 00:07:20,761 --> 00:07:23,400 function is also sometimes referred to as 159 00:07:23,430 --> 00:07:27,420 returned value. The last point in this regards 160 00:07:27,421 --> 00:07:29,970 is that when the function expects a return 161 00:07:29,971 --> 00:07:32,520 value, and the Rust compiler did not found any 162 00:07:32,521 --> 00:07:34,860 expression after which there is no semicolon, 163 00:07:35,130 --> 00:07:38,100 then it will also complain. For instance, if 164 00:07:38,101 --> 00:07:40,830 I add semicolon after the 165 00:07:40,831 --> 00:07:42,900 multiplication operation then there will be 166 00:07:42,901 --> 00:07:45,600 errors in the code saying remove semicolon. 167 00:07:46,170 --> 00:07:49,140 Let's move to the main function, now. I will 168 00:07:49,141 --> 00:07:51,090 run this function by adding a couple of lines 169 00:07:51,091 --> 00:07:54,270 of code in the main function. I will call 170 00:07:54,271 --> 00:07:56,280 this function with the inputs of 10 and 50. 171 00:07:56,340 --> 00:07:57,870 And we'll store the result value in the 172 00:07:57,871 --> 00:07:59,333 variable of answer. 173 00:07:59,334 --> 00:08:03,970 [No Audio] 174 00:08:03,971 --> 00:08:05,600 Let us add a simple print statement 175 00:08:05,601 --> 00:08:07,700 for printing the answer and execute. 176 00:08:07,701 --> 00:08:16,710 [No Audio] 177 00:08:16,711 --> 00:08:18,900 Let us now cover functions with multiple 178 00:08:18,901 --> 00:08:21,780 outputs. The syntax will be very similar 179 00:08:21,781 --> 00:08:24,090 to the function with outputs. To save some 180 00:08:24,091 --> 00:08:26,040 time, I will copy the function that we just 181 00:08:26,041 --> 00:08:27,390 wrote and we'll paste it. 182 00:08:27,400 --> 00:08:33,299 [No Audio] 183 00:08:33,300 --> 00:08:35,129 I will change its name to function with 184 00:08:35,130 --> 00:08:37,508 inputs and multiple outputs. 185 00:08:37,509 --> 00:08:43,740 [No Audio] 186 00:08:43,741 --> 00:08:46,710 To mention multiple outputs from the function 187 00:08:46,711 --> 00:08:49,200 we are required to use the tuples, which are 188 00:08:49,201 --> 00:08:51,300 specified using the smoooth brackets. 189 00:08:52,666 --> 00:08:55,181 Inside the brackets we may mention as many 190 00:08:55,182 --> 00:08:58,533 outputs as we like. Let us mention three outputs 191 00:08:58,534 --> 00:09:01,590 for now all having the same type of i32. 192 00:09:02,610 --> 00:09:04,830 The outputs are not necessarily of the same 193 00:09:04,831 --> 00:09:07,740 type, you are free to change their types. Now 194 00:09:07,741 --> 00:09:09,840 inside the body of the function, I will need 195 00:09:09,841 --> 00:09:13,933 to return a tuple with three values of type i32. 196 00:09:14,133 --> 00:09:16,080 I want the first value in the 197 00:09:16,140 --> 00:09:19,020 tuple to be the result of 198 00:09:19,021 --> 00:09:21,600 multiplication of two numbers, the second 199 00:09:21,601 --> 00:09:23,550 value to be the addition of the two values 200 00:09:23,700 --> 00:09:25,920 and the last value to be the subtraction of 201 00:09:25,921 --> 00:09:28,200 the first number from the second number. Let 202 00:09:28,201 --> 00:09:30,900 us mention these return values as a tuple. 203 00:09:32,970 --> 00:09:35,010 After the tuple I will skip adding the 204 00:09:35,011 --> 00:09:37,380 semicolon which will let the compiler know 205 00:09:37,381 --> 00:09:39,900 that this tuple is going to be the output or 206 00:09:39,901 --> 00:09:42,840 return value from the function. This way we 207 00:09:42,841 --> 00:09:45,210 are able to return multiple values which are 208 00:09:45,211 --> 00:09:48,540 basically residing inside the tuple. To use 209 00:09:48,541 --> 00:09:50,400 this function inside the code, I can either 210 00:09:50,401 --> 00:09:52,920 declare a tuple and receive all the values 211 00:09:52,950 --> 00:09:56,190 inside it or alternatively I can destructure 212 00:09:56,191 --> 00:09:58,890 its value and capture them inside individual 213 00:09:58,891 --> 00:10:01,833 variables. Let us learn both methods. 214 00:10:03,033 --> 00:10:05,700 First, let us destructure the values into the 215 00:10:05,701 --> 00:10:08,130 variables of multiplication addition and 216 00:10:08,131 --> 00:10:10,290 subtraction, I will call the function with 217 00:10:10,291 --> 00:10:11,730 the inputs of 10 and 15. 218 00:10:11,731 --> 00:10:20,910 [No Audio] 219 00:10:20,911 --> 00:10:22,860 Let us print the values now and execute. 220 00:10:22,861 --> 00:10:29,910 [No Audio] 221 00:10:29,911 --> 00:10:31,560 The function is working correctly and 222 00:10:31,561 --> 00:10:34,590 producing the right results. Let us look at 223 00:10:34,591 --> 00:10:37,320 the second method now, I will use a single 224 00:10:37,321 --> 00:10:39,330 tuple to receive all the outputs from the 225 00:10:39,331 --> 00:10:42,333 function, I will store it in a variable of result. 226 00:10:42,334 --> 00:10:47,790 [No Audio] 227 00:10:47,791 --> 00:10:50,280 To access the individual values inside the 228 00:10:50,281 --> 00:10:53,800 result tuple, we will use the .index notation. 229 00:10:53,801 --> 00:10:55,470 So, let us display the individual 230 00:10:55,471 --> 00:10:56,933 values and execute. 231 00:10:56,934 --> 00:11:03,120 [No Audio] 232 00:11:03,121 --> 00:11:05,400 Okay that was it with regards to functions 233 00:11:05,401 --> 00:11:08,580 for now, we will now cover the topic of code 234 00:11:08,581 --> 00:11:10,680 blocks, which is somewhat related to 235 00:11:10,681 --> 00:11:13,950 functions. A code block is some code which is 236 00:11:13,951 --> 00:11:16,560 enclosed inside curly brackets and do some 237 00:11:16,561 --> 00:11:20,200 computations with possible output result. 238 00:11:20,766 --> 00:11:22,080 They are like functions in the 239 00:11:22,081 --> 00:11:24,420 sense that they have a body 240 00:11:24,421 --> 00:11:27,270 and does some specific computation. However, 241 00:11:27,420 --> 00:11:30,300 they do not have an explicit name and we may 242 00:11:30,301 --> 00:11:33,000 not be always able to use them over and over 243 00:11:33,001 --> 00:11:35,340 again, since they have some relevance to 244 00:11:35,341 --> 00:11:37,440 function. So, therefore, we cover them here. 245 00:11:38,580 --> 00:11:40,710 A code block is defined by having some code 246 00:11:40,711 --> 00:11:43,620 inside curly brackets, let us define a 247 00:11:43,621 --> 00:11:45,827 couple of string variables inside the 248 00:11:45,828 --> 00:11:47,100 body of the code block. 249 00:11:47,101 --> 00:11:53,760 [No Audio] 250 00:11:53,761 --> 00:11:56,700 Next, I will use a format macro to make 251 00:11:56,701 --> 00:11:58,740 another string from the two strings. 252 00:11:58,741 --> 00:12:06,120 [No Audio] 253 00:12:06,121 --> 00:12:09,420 I intentionally do not add a semicolon after 254 00:12:09,421 --> 00:12:12,210 the last statement inside the code block and 255 00:12:12,211 --> 00:12:14,233 the reason will become clear to us in a minute. 256 00:12:14,833 --> 00:12:16,890 I want the result from this code 257 00:12:16,891 --> 00:12:19,320 block to be stored in a variable. So at the 258 00:12:19,321 --> 00:12:21,480 start of the code block, I will declare a 259 00:12:21,481 --> 00:12:23,733 variable with the name of full_name 260 00:12:23,933 --> 00:12:26,800 and we'll assign the whole code block to it. 261 00:12:28,433 --> 00:12:30,500 This means that the return value 262 00:12:30,501 --> 00:12:32,190 from the code block will be 263 00:12:32,191 --> 00:12:34,890 assigned to the variable full name. The 264 00:12:34,891 --> 00:12:37,110 return value from the code block is basically 265 00:12:37,111 --> 00:12:39,390 the statement that do not have a semicolon 266 00:12:39,391 --> 00:12:42,360 after it. In this case, it is the format 267 00:12:42,361 --> 00:12:45,210 macro. Let us display the full name after the 268 00:12:45,211 --> 00:12:46,650 code block and execute. 269 00:12:46,651 --> 00:12:54,210 [No Audio] 270 00:12:54,211 --> 00:12:57,240 Okay, great small chunks of code blocks can 271 00:12:57,241 --> 00:12:59,640 be quite handy in arranging your code and 272 00:12:59,641 --> 00:13:02,160 initializing variable and isolating small 273 00:13:02,161 --> 00:13:04,033 computations from the Rust of the code. 274 00:13:04,400 --> 00:13:07,290 This will bring more clarity to the code. Let us 275 00:13:07,291 --> 00:13:09,210 now move to the last topic in today's 276 00:13:09,211 --> 00:13:11,310 learning agenda, which is to learn how to 277 00:13:11,311 --> 00:13:12,633 take inputs in Rust. 278 00:13:14,100 --> 00:13:15,600 Receiving inputs from 279 00:13:15,601 --> 00:13:17,850 the user is important for user interaction 280 00:13:17,851 --> 00:13:19,860 perspective, because sooner or later, you 281 00:13:19,861 --> 00:13:21,690 should be expecting that the program you 282 00:13:21,691 --> 00:13:23,910 write will have an interaction with the user. 283 00:13:24,930 --> 00:13:27,510 Same as in other programming languages. The 284 00:13:27,511 --> 00:13:29,910 earliest use the standard input output 285 00:13:29,911 --> 00:13:33,166 library, to get input from the user. The read_line 286 00:13:33,167 --> 00:13:34,350 function defined in the 287 00:13:34,351 --> 00:13:36,570 standard library is used to read the user 288 00:13:36,571 --> 00:13:39,120 input, in the form of string which we may then 289 00:13:39,121 --> 00:13:41,550 convert into any other format as per our 290 00:13:41,551 --> 00:13:44,430 requirements. Since the read_line reads the 291 00:13:44,431 --> 00:13:47,580 user input into the form of a string, so we 292 00:13:47,581 --> 00:13:49,800 will first create an empty string which will 293 00:13:49,801 --> 00:13:52,290 store the value of the user input, and we will 294 00:13:52,291 --> 00:13:54,690 pass it to the read_line function. 295 00:13:54,691 --> 00:13:59,850 [No Audio] 296 00:13:59,851 --> 00:14:01,890 Next I will use the read_line 297 00:14:01,891 --> 00:14:03,750 function which is part of the standard 298 00:14:03,751 --> 00:14:06,390 library and to access it, we will use the 299 00:14:06,391 --> 00:14:11,000 syntax of std::io::stdin 300 00:14:11,833 --> 00:14:13,300 followed by smooth brackets. 301 00:14:13,301 --> 00:14:15,600 [No Audio] 302 00:14:15,601 --> 00:14:18,166 And then I will mention the read_line function 303 00:14:18,433 --> 00:14:20,610 with the input of the string that we 304 00:14:20,611 --> 00:14:23,430 just created. This function requires a 305 00:14:23,431 --> 00:14:26,070 mutable reference to the string. We will 306 00:14:26,071 --> 00:14:28,530 cover mutable references in the next section. 307 00:14:28,770 --> 00:14:31,350 But for now, just note that it is a reference 308 00:14:31,351 --> 00:14:33,480 to the actual data. But with this type of 309 00:14:33,481 --> 00:14:36,000 reference, you can change the actual contents 310 00:14:36,030 --> 00:14:40,320 residing inside the data. This means that the 311 00:14:40,321 --> 00:14:42,960 input string we pass to it will be changed to 312 00:14:42,961 --> 00:14:45,600 using this function to the value of the user input. 313 00:14:46,000 --> 00:14:48,450 Next in case of any reading error, 314 00:14:48,451 --> 00:14:51,450 which generally is very rare, and would come 315 00:14:51,451 --> 00:14:54,030 from the underlying operating system. Then in 316 00:14:54,031 --> 00:14:56,670 that case, there will be an error, which we 317 00:14:56,671 --> 00:14:58,800 want to handle by using another function 318 00:14:58,801 --> 00:15:01,590 called expected. We will call this function 319 00:15:01,591 --> 00:15:05,130 with with a suitable input message. In case 320 00:15:05,131 --> 00:15:07,860 of error the input message that we pass to 321 00:15:07,861 --> 00:15:09,540 this function will be displayed on the 322 00:15:09,541 --> 00:15:12,420 terminal. If we remove this function the code 323 00:15:12,421 --> 00:15:14,520 will still execute but the Rust compiler will 324 00:15:14,521 --> 00:15:17,940 give us a warning. Please note that the return 325 00:15:17,941 --> 00:15:20,220 value from the function read_line 326 00:15:20,221 --> 00:15:24,120 is of result enum type more details of enums 327 00:15:24,121 --> 00:15:26,370 will be covered later on, but for now, it is 328 00:15:26,371 --> 00:15:28,500 sufficient to know that the result will have 329 00:15:28,501 --> 00:15:31,400 two possible values that is error or &ok. 330 00:15:31,500 --> 00:15:33,390 An okay variant indicate that the 331 00:15:33,391 --> 00:15:36,600 operation was successful and inside okay, we 332 00:15:36,601 --> 00:15:39,033 have the successfully generated value. 333 00:15:39,466 --> 00:15:42,240 The error variant means that the operation failed 334 00:15:42,540 --> 00:15:45,450 and the error contains information about 335 00:15:45,451 --> 00:15:48,990 how or why the operation failed. Now, once we 336 00:15:48,991 --> 00:15:51,570 have the user input, we will next convert it 337 00:15:51,571 --> 00:15:54,360 to a suitable type such as an integer float 338 00:15:54,361 --> 00:15:57,870 char or other to convert to different types, 339 00:15:58,110 --> 00:16:00,300 we will mention the type in which we want to 340 00:16:00,301 --> 00:16:03,633 see the user input. I will consider an f64 format. 341 00:16:03,866 --> 00:16:06,766 So I will write colon followed by f64 342 00:16:06,767 --> 00:16:11,220 after the variable name. Next, I will use 343 00:16:11,221 --> 00:16:13,980 the trim function which will trim the 344 00:16:13,981 --> 00:16:16,800 trailing whitespace is from the string. Next 345 00:16:16,801 --> 00:16:18,870 I will use the parse function on the 346 00:16:18,900 --> 00:16:22,350 resultant trim function. This function parses 347 00:16:22,351 --> 00:16:24,480 the string into another type which in this 348 00:16:24,481 --> 00:16:27,450 case is f64. Finally, we will add an 349 00:16:27,451 --> 00:16:30,360 optional function or expect which will be 350 00:16:30,361 --> 00:16:33,600 executed in case of errors. This will be 351 00:16:33,601 --> 00:16:35,940 executed in case the parse function 352 00:16:35,941 --> 00:16:38,130 is not being able to convert the input string 353 00:16:38,160 --> 00:16:41,610 into the required data type. Let us add a 354 00:16:41,611 --> 00:16:43,410 print statement for displaying the value of 355 00:16:43,411 --> 00:16:45,004 the input and execute. 356 00:16:45,005 --> 00:16:50,850 [No Audio] 357 00:16:50,851 --> 00:16:53,280 The program is asking for the input, I will 358 00:16:53,281 --> 00:16:55,920 provide a value and it prints the same value 359 00:16:55,921 --> 00:16:58,890 back to the screen. Let us cargo run once 360 00:16:58,891 --> 00:17:00,720 more time and this time I will provide a 361 00:17:00,721 --> 00:17:05,280 wrong value. It returns an error message that 362 00:17:05,281 --> 00:17:08,910 is invalid input. If we want to obtain input 363 00:17:08,940 --> 00:17:11,566 in other formats, we may change the type of 364 00:17:11,567 --> 00:17:14,460 f64 to other formats and receive inputs in 365 00:17:14,461 --> 00:17:17,490 different formats. That brings us to the end 366 00:17:17,491 --> 00:17:19,680 of this tutorial and also to the end of this 367 00:17:19,681 --> 00:17:22,170 section. We have focused on covering some 368 00:17:22,171 --> 00:17:24,300 basic programming concepts in this section of 369 00:17:24,359 --> 00:17:27,210 course, in the upcoming section, we will be 370 00:17:27,211 --> 00:17:29,340 covering the Rust central concept of 371 00:17:29,370 --> 00:17:32,070 ownership. I'm quite excited about the next 372 00:17:32,071 --> 00:17:34,530 section, and I believe you will come fresh to 373 00:17:34,531 --> 00:17:36,840 attend that section also for building strong 374 00:17:36,841 --> 00:17:39,810 foundations. With this we end this tutorial 375 00:17:39,811 --> 00:17:42,180 and section see you again and until next 376 00:17:42,181 --> 00:17:44,640 tutorial. Enjoy Rust programming. 377 00:17:44,641 --> 00:17:51,466 [No Audio]