File tree Expand file tree Collapse file tree 1 file changed +21
-4
lines changed
src/librustc_error_codes/error_codes Expand file tree Collapse file tree 1 file changed +21
-4
lines changed Original file line number Diff line number Diff line change 1
- It is not allowed to manually call destructors in Rust. It is also not
2
- necessary to do this since ` drop ` is called automatically whenever a value goes
3
- out of scope.
1
+ It is not allowed to manually call destructors in Rust.
4
2
5
- Here's an example of this error :
3
+ Erroneous code example:
6
4
7
5
``` compile_fail,E0040
8
6
struct Foo {
@@ -20,3 +18,22 @@ fn main() {
20
18
x.drop(); // error: explicit use of destructor method
21
19
}
22
20
```
21
+
22
+ It is unnecessary to do this since ` drop ` is called automatically whenever a
23
+ value goes out of scope. However, if you really need to drop a value by hand,
24
+ you can use the ` std::mem::drop ` function:
25
+
26
+ ```
27
+ struct Foo {
28
+ x: i32,
29
+ }
30
+ impl Drop for Foo {
31
+ fn drop(&mut self) {
32
+ println!("kaboom");
33
+ }
34
+ }
35
+ fn main() {
36
+ let mut x = Foo { x: -7 };
37
+ drop(x); // ok!
38
+ }
39
+ ```
You can’t perform that action at this time.
0 commit comments