]> git.lizzy.rs Git - rust.git/blob - src/docs/let_underscore_drop.txt
Rollup merge of #104595 - compiler-errors:poly-existential-predicate, r=lcnr
[rust.git] / src / docs / let_underscore_drop.txt
1 ### What it does
2 Checks for `let _ = <expr>`
3 where expr has a type that implements `Drop`
4
5 ### Why is this bad?
6 This statement immediately drops the initializer
7 expression instead of extending its lifetime to the end of the scope, which
8 is often not intended. To extend the expression's lifetime to the end of the
9 scope, use an underscore-prefixed name instead (i.e. _var). If you want to
10 explicitly drop the expression, `std::mem::drop` conveys your intention
11 better and is less error-prone.
12
13 ### Example
14 ```
15 {
16     let _ = DroppableItem;
17     //                   ^ dropped here
18     /* more code */
19 }
20 ```
21
22 Use instead:
23 ```
24 {
25     let _droppable = DroppableItem;
26     /* more code */
27     // dropped at end of scope
28 }
29 ```