...

/

Introduction to Pointers

Introduction to Pointers

In this lesson, you will learn about Rust pointers.

What are pointers?

Pointers are used to point to memory locations. In Rust, there are three kinds of pointers

  • Raw Pointers
  • References
  • Smart Pointers

Pointers tend to have memory safety issues, so you may not be using pointers a whole lot while programming in Rust. Still, it’s important to know them when building complex data structures or implementing algorithms.

Raw pointers

Raw pointers can be assigned raw addresses but dereferencing them is considered an unsafe operation. Here’s an example

let z: u32 = 55; 
let p = z as *const u32;

In the above code, We are creating a raw pointer p that is pointing to z. If you try to dereference p, you will get the following error.

error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block
  -->
...
Access this course and 1400+ top-rated courses and projects.