You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/expressions/operator-expr.md
+28Lines changed: 28 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -189,6 +189,34 @@ let y = &mut 9;
189
189
assert_eq!(*y, 11);
190
190
```
191
191
192
+
> [!NOTE]
193
+
> The [temporary scopes][temporary scope] of `x` in `*x` and `*std::ops::Deref::deref(&x)` are not always the same in all immutable place expression contexts.
194
+
> Likewise, the temporary scopes of `x` in `*x` and `*std::ops::DerefMut::deref_mut(&mut x)` are not always the same in all mutable place expression contexts.
195
+
>
196
+
> If `*x` has an [extended][temporary lifetime extension] temporary scope, then `x` does as well ([destructors.scope.lifetime-extension.sub-expressions]).
197
+
> However, the same does not apply to `*std::ops::Deref::deref(&x)` or `*std::ops::DerefMut::deref_mut(&mut x)`.
198
+
>
199
+
> For example, this affects automatically reference-counted pointers:
200
+
>
201
+
> ```rust
202
+
> usestd::rc::Rc;
203
+
>
204
+
> letrc=Rc::new(());
205
+
> // The reference count starts at 1.
206
+
> assert_eq!(Rc::strong_count(&rc), 1);
207
+
>
208
+
> letrefx=*std::ops::Deref::deref(&rc.clone());
209
+
> // `rc.clone()` increments the reference count, but its result is
210
+
> // dropped at the end of the statement, so the reference count goes
211
+
> // back to 1.
212
+
> assert_eq!(Rc::strong_count(&rc), 1);
213
+
>
214
+
> letrefy=*rc.clone();
215
+
> // In this case, the lifetime of `rc.clone()` is extended to the end
216
+
> // of the block, so the reference count stays at 2.
0 commit comments