Skip to content

Commit 91f1444

Browse files
authored
Merge pull request #2075 from dianne/deref-box-special-case
Add special cases and links relating to dereferencing boxes
2 parents 1d674cb + d740b23 commit 91f1444

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
@@ -369,6 +369,7 @@ They are never allowed before:
369369
* [Range] expressions.
370370
* Binary operator expressions ([ArithmeticOrLogicalExpression], [ComparisonExpression], [LazyBooleanExpression], [TypeCastExpression], [AssignmentExpression], [CompoundAssignmentExpression]).
371371

372+
[`Box<T>`]: special-types-and-traits.md#boxt
372373
[`Copy`]: special-types-and-traits.md#copy
373374
[`Drop`]: special-types-and-traits.md#drop
374375
[`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
@@ -164,10 +164,13 @@ r[expr.deref.intro]
164164
The `*` (dereference) operator is also a unary prefix operator.
165165

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

169169
r[expr.deref.mut]
170-
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.
170+
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.
171+
172+
r[expr.deref.box]
173+
When applied to a [`Box`], the resultant place may be [moved from].
171174

172175
r[expr.deref.safety]
173176
Dereferencing a raw pointer requires `unsafe`.
@@ -176,11 +179,14 @@ r[expr.deref.traits]
176179
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.
177180

178181
```rust
182+
# struct NoCopy;
179183
let x = &7;
180184
assert_eq!(*x, 7);
181185
let y = &mut 9;
182186
*y = 11;
183187
assert_eq!(*y, 11);
188+
let z = Box::new(NoCopy);
189+
let _: NoCopy = *z;
184190
```
185191

186192
r[expr.try]
@@ -1208,6 +1214,7 @@ As with normal assignment expressions, compound assignment expressions always pr
12081214
> [!WARNING]
12091215
> Avoid writing code that depends on the evaluation order of operands in compound assignments as it can be unusual and surprising.
12101216
1217+
[`Box`]: ../special-types-and-traits.md#boxt
12111218
[`Try`]: core::ops::Try
12121219
[autoref]: expr.method.candidate-receivers-refs
12131220
[copies or moves]: ../expressions.md#moved-and-copied-types
@@ -1222,6 +1229,7 @@ As with normal assignment expressions, compound assignment expressions always pr
12221229
[logical not]: ../types/boolean.md#logical-not
12231230
[logical or]: ../types/boolean.md#logical-or
12241231
[logical xor]: ../types/boolean.md#logical-xor
1232+
[moved from]: expr.move.movable-place
12251233
[mutable]: ../expressions.md#mutability
12261234
[place expression]: ../expressions.md#place-expressions-and-value-expressions
12271235
[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)