]> git.proxmox.com Git - rustc.git/blame - src/librustc_error_codes/error_codes/E0040.md
New upstream version 1.47.0+dfsg1
[rustc.git] / src / librustc_error_codes / error_codes / E0040.md
CommitLineData
60c5eb7d
XL
1It is not allowed to manually call destructors in Rust.
2
3Erroneous code example:
4
5```compile_fail,E0040
6struct Foo {
7 x: i32,
8}
9
10impl Drop for Foo {
11 fn drop(&mut self) {
12 println!("kaboom");
13 }
14}
15
16fn main() {
17 let mut x = Foo { x: -7 };
18 x.drop(); // error: explicit use of destructor method
19}
20```
21
22It is unnecessary to do this since `drop` is called automatically whenever a
23value goes out of scope. However, if you really need to drop a value by hand,
24you can use the `std::mem::drop` function:
25
26```
27struct Foo {
28 x: i32,
29}
30impl Drop for Foo {
31 fn drop(&mut self) {
32 println!("kaboom");
33 }
34}
35fn main() {
36 let mut x = Foo { x: -7 };
37 drop(x); // ok!
38}
39```