]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_error_codes/src/error_codes/E0040.md
Merge commit '370c397ec9169809e5ad270079712e0043514240' into sync_cg_clif-2022-03-20
[rust.git] / compiler / rustc_error_codes / src / error_codes / E0040.md
1 It is not allowed to manually call destructors in Rust.
2
3 Erroneous code example:
4
5 ```compile_fail,E0040
6 struct Foo {
7     x: i32,
8 }
9
10 impl Drop for Foo {
11     fn drop(&mut self) {
12         println!("kaboom");
13     }
14 }
15
16 fn main() {
17     let mut x = Foo { x: -7 };
18     x.drop(); // error: explicit use of destructor method
19 }
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 ```