Lesson 38 of 50 · rust
Unsafe Rust
Duration: 15 minutes
Lesson 38 of 50 · rust
Duration: 15 minutes
unsafe blocks allow you to perform operations the compiler cannot guarantee safety for, such as dereferencing raw pointers.\n\nrust\nfn main() {\n let mut num = 5;\n let r1 = &num as *const i32;\n let r2 = &mut num as *mut i32;\n unsafe {\n println!(\"r1 points to: {}\", *r1);\n *r2 = 10;\n }\n println!(\"num is now: {}\", num);\n}\n\n\nUse unsafe sparingly and encapsulate it in safe abstractions.