Skip to content

Commit d740b23

Browse files
committed
Add special cases and links relating to dereferencing boxes
1 parent e122eef commit d740b23

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

src/expressions.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ They are never allowed before:
387387
* [Range] expressions.
388388
* Binary operator expressions ([ArithmeticOrLogicalExpression], [ComparisonExpression], [LazyBooleanExpression], [TypeCastExpression], [AssignmentExpression], [CompoundAssignmentExpression]).
389389

390+
[`Box<T>`]: special-types-and-traits.md#boxt
390391
[`Copy`]: special-types-and-traits.md#copy
391392
[`Drop`]: special-types-and-traits.md#drop
392393
[`if let`]: expressions/if-expr.md#if-let-patterns

src/expressions/operator-expr.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,13 @@ r[expr.deref.intro]
170170
The `*` (dereference) operator is also a unary prefix operator.
171171

172172
r[expr.deref.result]
173-
When applied to a [pointer](../types/pointer.md) it denotes the pointed-to location.
173+
When applied to a [pointer](../types/pointer.md) or [`Box`], it denotes the pointed-to location.
174174

175175
r[expr.deref.mut]
176-
If the expression is of type `&mut T` or `*mut T`, and is either a local variable, a (nested) field of a local variable or is a mutable [place expression], then the resulting memory location can be assigned to.
176+
If the expression is of type `&mut T`, `*mut T`, or `Box<T>`, and is either a local variable, a (nested) field of a local variable or is a mutable [place expression], then the resulting memory location can be assigned to.
177+
178+
r[expr.deref.box]
179+
When applied to a [`Box`], the resultant place may be [moved from].
177180

178181
r[expr.deref.safety]
179182
Dereferencing a raw pointer requires `unsafe`.
@@ -182,11 +185,14 @@ r[expr.deref.traits]
182185
On non-pointer types `*x` is equivalent to `*std::ops::Deref::deref(&x)` in an [immutable place expression context](../expressions.md#mutability) and `*std::ops::DerefMut::deref_mut(&mut x)` in a mutable place expression context.
183186

184187
```rust
188+
# struct NoCopy;
185189
let x = &7;
186190
assert_eq!(*x, 7);
187191
let y = &mut 9;
188192
*y = 11;
189193
assert_eq!(*y, 11);
194+
let z = Box::new(NoCopy);
195+
let _: NoCopy = *z;
190196
```
191197

192198
r[expr.try]
@@ -1083,6 +1089,7 @@ As with normal assignment expressions, compound assignment expressions always pr
10831089
> [!WARNING]
10841090
> Avoid writing code that depends on the evaluation order of operands in compound assignments as it can be unusual and surprising.
10851091
1092+
[`Box`]: ../special-types-and-traits.md#boxt
10861093
[`Try`]: core::ops::Try
10871094
[autoref]: expr.method.candidate-receivers-refs
10881095
[copies or moves]: ../expressions.md#moved-and-copied-types
@@ -1097,6 +1104,7 @@ As with normal assignment expressions, compound assignment expressions always pr
10971104
[logical not]: ../types/boolean.md#logical-not
10981105
[logical or]: ../types/boolean.md#logical-or
10991106
[logical xor]: ../types/boolean.md#logical-xor
1107+
[moved from]: expr.move.movable-place
11001108
[mutable]: ../expressions.md#mutability
11011109
[place expression]: ../expressions.md#place-expressions-and-value-expressions
11021110
[assignee expression]: ../expressions.md#place-expressions-and-value-expressions

src/special-types-and-traits.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ r[lang-types.box.intro]
1414
defined types.
1515

1616
r[lang-types.box.deref]
17-
* The [dereference operator] for `Box<T>` produces a place which can be moved
18-
from. This means that the `*` operator and the destructor of `Box<T>` are
17+
* The [dereference operator] for `Box<T>` produces a place which can be [moved
18+
from]. This means that the `*` operator and the destructor of `Box<T>` are
1919
built-in to the language.
2020

2121
r[lang-types.box.receiver]
@@ -239,6 +239,7 @@ These implicit `Sized` bounds may be relaxed by using the special `?Sized` bound
239239
[main function]: crates-and-source-files.md#main-functions
240240
[Methods]: items/associated-items.md#associated-functions-and-methods
241241
[method resolution]: expressions/method-call-expr.md
242+
[moved from]: expr.move.movable-place
242243
[operators]: expressions/operator-expr.md
243244
[orphan rules]: items/implementations.md#trait-implementation-coherence
244245
[`static` items]: items/static-items.md

0 commit comments

Comments
 (0)