1# Pointer types
2
3All pointers in Rust are explicit first-class values.
4They can be moved or copied, stored into data structs, and returned from functions.
5
6## References (`&` and `&mut`)
7
8> **<sup>Syntax</sup>**\
9> _ReferenceType_ :\
10> &nbsp;&nbsp; `&` [_Lifetime_]<sup>?</sup> `mut`<sup>?</sup> [_TypeNoBounds_]
11
12### Shared references (`&`)
13
14These point to memory _owned by some other value_.
15When a shared reference to a value is created it prevents direct mutation of the value.
16[Interior mutability] provides an exception for this in certain circumstances.
17As the name suggests, any number of shared references to a value may exist.
18A shared reference type is written `&type`, or `&'a type` when you need to specify an explicit lifetime.
19Copying a reference is a "shallow" operation:
20it involves only copying the pointer itself, that is, pointers are `Copy`.
21Releasing a reference has no effect on the value it points to, but referencing of a [temporary value] will keep it alive during the scope of the reference itself.
22
23### Mutable references (`&mut`)
24
25These also point to memory owned by some other value.
26A mutable reference type is written `&mut type` or `&'a mut type`.
27A mutable reference (that hasn't been borrowed) is the only way to access the value it points to, so is not `Copy`.
28
29## Raw pointers (`*const` and `*mut`)
30
31> **<sup>Syntax</sup>**\
32> _RawPointerType_ :\
33> &nbsp;&nbsp; `*` ( `mut` | `const` ) [_TypeNoBounds_]
34
35Raw pointers are pointers without safety or liveness guarantees.
36Raw pointers are written as `*const T` or `*mut T`.
37For example `*const i32` means a raw pointer to a 32-bit integer.
38Copying or dropping a raw pointer has no effect on the lifecycle of any other value.
39Dereferencing a raw pointer is an [`unsafe` operation].
40This can also be used to convert a raw pointer to a reference by reborrowing it (`&*` or `&mut *`).
41Raw pointers are generally discouraged in Rust code;
42they exist to support interoperability with foreign code, and writing performance-critical or low-level functions.
43
44When comparing raw pointers they are compared by their address, rather than by what they point to.
45When comparing raw pointers to [dynamically sized types] they also have their additional data compared.
46
47Raw pointers can be created directly using [`core::ptr::addr_of!`] for `*const` pointers and [`core::ptr::addr_of_mut!`] for `*mut` pointers.
48
49## Smart Pointers
50
51The standard library contains additional 'smart pointer' types beyond references and raw pointers.
52
53[`core::ptr::addr_of!`]: ../../core/ptr/macro.addr_of.html
54[`core::ptr::addr_of_mut!`]: ../../core/ptr/macro.addr_of_mut.html
55[Interior mutability]: ../interior-mutability.md
56[_Lifetime_]: ../trait-bounds.md
57[_TypeNoBounds_]: ../types.md#type-expressions
58[`unsafe` operation]: ../unsafety.md
59[dynamically sized types]: ../dynamically-sized-types.md
60[temporary value]: ../expressions.md#temporaries
61