Skip to content

Commit 942a7ab

Browse files
authored
Merge pull request rust-lang#993 from Havvy/pointer-addr-of
Link to ptr::addr_of on raw pointer docs
2 parents 220ca54 + 4664361 commit 942a7ab

File tree

1 file changed

+28
-27
lines changed

1 file changed

+28
-27
lines changed

src/types/pointer.md

+28-27
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Pointer types
22

3-
All pointers in Rust are explicit first-class values. They can be moved or
4-
copied, stored into data structs, and returned from functions.
3+
All pointers in Rust are explicit first-class values.
4+
They can be moved or copied, stored into data structs, and returned from functions.
55

66
## References (`&` and `&mut`)
77

@@ -11,46 +11,47 @@ copied, stored into data structs, and returned from functions.
1111
1212
### Shared references (`&`)
1313

14-
These point to memory _owned by some other value_. When a shared reference to
15-
a value is created it prevents direct mutation of the value. [Interior
16-
mutability] provides an exception for this in certain circumstances. As the
17-
name suggests, any number of shared references to a value may exist. A shared
18-
reference type is written `&type`, or `&'a type` when you need to specify an
19-
explicit lifetime. Copying a reference is a "shallow" operation: it involves
20-
only copying the pointer itself, that is, pointers are `Copy`. Releasing a
21-
reference has no effect on the value it points to, but referencing of a
22-
[temporary value] will keep it alive during the scope of the reference itself.
14+
These point to memory _owned by some other value_.
15+
When 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.
17+
As the name suggests, any number of shared references to a value may exist.
18+
A shared reference type is written `&type`, or `&'a type` when you need to specify an explicit lifetime.
19+
Copying a reference is a "shallow" operation:
20+
it involves only copying the pointer itself, that is, pointers are `Copy`.
21+
Releasing 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.
2322

2423
### Mutable references (`&mut`)
2524

26-
These also point to memory owned by some other value. A mutable reference type
27-
is written `&mut type` or `&'a mut type`. A mutable reference (that hasn't been
28-
borrowed) is the only way to access the value it points to, so is not `Copy`.
25+
These also point to memory owned by some other value.
26+
A mutable reference type is written `&mut type` or `&'a mut type`.
27+
A mutable reference (that hasn't been borrowed) is the only way to access the value it points to, so is not `Copy`.
2928

3029
## Raw pointers (`*const` and `*mut`)
3130

3231
> **<sup>Syntax</sup>**\
3332
> _RawPointerType_ :\
3433
> &nbsp;&nbsp; `*` ( `mut` | `const` ) [_TypeNoBounds_]
3534
36-
Raw pointers are pointers without safety or liveness guarantees. Raw pointers
37-
are written as `*const T` or `*mut T`, for example `*const i32` means a raw
38-
pointer to a 32-bit integer. Copying or dropping a raw pointer has no effect
39-
on the lifecycle of any other value. Dereferencing a raw pointer is an
40-
[`unsafe` operation], this can also be used to convert a raw pointer to a
41-
reference by reborrowing it (`&*` or `&mut *`). Raw pointers are generally
42-
discouraged in Rust code; they exist to support interoperability with foreign
43-
code, and writing performance-critical or low-level functions.
35+
Raw pointers are pointers without safety or liveness guarantees.
36+
Raw pointers are written as `*const T` or `*mut T`.
37+
For example `*const i32` means a raw pointer to a 32-bit integer.
38+
Copying or dropping a raw pointer has no effect on the lifecycle of any other value.
39+
Dereferencing a raw pointer is an [`unsafe` operation].
40+
This can also be used to convert a raw pointer to a reference by reborrowing it (`&*` or `&mut *`).
41+
Raw pointers are generally discouraged in Rust code;
42+
they exist to support interoperability with foreign code, and writing performance-critical or low-level functions.
4443

45-
When comparing raw pointers they are compared by their address, rather than by
46-
what they point to. When comparing raw pointers to [dynamically sized types] they
47-
also have their additional data compared.
44+
When comparing raw pointers they are compared by their address, rather than by what they point to.
45+
When comparing raw pointers to [dynamically sized types] they also have their additional data compared.
46+
47+
Raw pointers can be created directly using [`core::ptr::addr_of!`] for `*const` pointers and [`core::ptr::addr_of_mut!`] for `*mut` pointers.
4848

4949
## Smart Pointers
5050

51-
The standard library contains additional 'smart pointer' types beyond references
52-
and raw pointers.
51+
The standard library contains additional 'smart pointer' types beyond references and raw pointers.
5352

53+
[`core::ptr::addr_of!`]: ../../core/ptr/macro.addr_of.html
54+
[`core::ptr::addr_of_mut!`]: ../../core/ptr/macro.addr_of_mut.html
5455
[Interior mutability]: ../interior-mutability.md
5556
[_Lifetime_]: ../trait-bounds.md
5657
[_TypeNoBounds_]: ../types.md#type-expressions

0 commit comments

Comments
 (0)