1 00:00:05,940 --> 00:00:10,200 Writing Unsafe Code in Rust is actually really easy to do. 2 00:00:10,530 --> 00:00:17,220 All we have to do is place our code inside of an unsafe block to just to let the compiler know we are 3 00:00:17,220 --> 00:00:19,560 about to execute unsafe code. 4 00:00:19,860 --> 00:00:21,960 So let's just see a quick example of this. 5 00:00:21,960 --> 00:00:32,520 So we're going to create an immutable variable called NUM and now we are going to create a raw pointer. 6 00:00:34,180 --> 00:00:39,160 To these to that variable which is not unsafe to do. 7 00:00:39,190 --> 00:00:40,120 We are very. 8 00:00:41,090 --> 00:00:43,250 Capable of doing that. 9 00:00:47,810 --> 00:00:51,350 And we can even print out the address. 10 00:00:54,730 --> 00:00:56,230 Of these variables. 11 00:01:00,440 --> 00:01:02,930 Let me copy and paste. 12 00:01:08,910 --> 00:01:10,890 One or two. 13 00:01:15,290 --> 00:01:23,120 So now we can run this and see that our address is point is printed out and it points back to the num 14 00:01:23,120 --> 00:01:24,080 of five. 15 00:01:24,410 --> 00:01:30,350 But if we try to d reference this, then it becomes an unsafe operation. 16 00:01:31,040 --> 00:01:34,640 And if we try to run it, we already know it's not going to run. 17 00:01:35,330 --> 00:01:42,690 It says DX reference of raw pointer is unsafe and requires unsafe function or block. 18 00:01:42,710 --> 00:01:51,950 So all we have to do in order to run this is just say unsafe and then print it and then put the unsafe 19 00:01:51,950 --> 00:01:54,110 code block into. 20 00:01:55,540 --> 00:01:56,920 That code block. 21 00:01:58,600 --> 00:02:05,980 So now if we run it, we see that we were able to successfully reference our raw pointer and print out 22 00:02:05,980 --> 00:02:07,240 the value of five. 23 00:02:09,450 --> 00:02:16,680 And just because you might use unsafe code in a function, it does not mean that the entire function 24 00:02:16,680 --> 00:02:17,720 has to be unsafe. 25 00:02:17,730 --> 00:02:26,340 So notice that all we did here was wrap the code that was considered unsafe and the unsafe code block. 26 00:02:26,640 --> 00:02:31,860 So you can simply just use unsafe blocks explicitly where appropriate. 27 00:02:31,860 --> 00:02:39,450 There is no need to wrap everything and an unsafe block if only one or two or however many lines is 28 00:02:39,450 --> 00:02:41,880 executing the unsafe code. 29 00:02:41,880 --> 00:02:50,190 That way you can continue to maximize the borrow checker and ownership model that Rust provides for 30 00:02:50,190 --> 00:02:50,490 us.