]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_error_codes/src/error_codes/E0600.md
Rollup merge of #93556 - dtolnay:trailingcomma, r=cjgillot
[rust.git] / compiler / rustc_error_codes / src / error_codes / E0600.md
1 An unary operator was used on a type which doesn't implement it.
2
3 Erroneous code example:
4
5 ```compile_fail,E0600
6 enum Question {
7     Yes,
8     No,
9 }
10
11 !Question::Yes; // error: cannot apply unary operator `!` to type `Question`
12 ```
13
14 In this case, `Question` would need to implement the `std::ops::Not` trait in
15 order to be able to use `!` on it. Let's implement it:
16
17 ```
18 use std::ops::Not;
19
20 enum Question {
21     Yes,
22     No,
23 }
24
25 // We implement the `Not` trait on the enum.
26 impl Not for Question {
27     type Output = bool;
28
29     fn not(self) -> bool {
30         match self {
31             Question::Yes => false, // If the `Answer` is `Yes`, then it
32                                     // returns false.
33             Question::No => true, // And here we do the opposite.
34         }
35     }
36 }
37
38 assert_eq!(!Question::Yes, false);
39 assert_eq!(!Question::No, true);
40 ```