]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_error_codes/src/error_codes/E0118.md
New upstream version 1.64.0+dfsg1
[rustc.git] / compiler / rustc_error_codes / src / error_codes / E0118.md
CommitLineData
1b1a35ee
XL
1An inherent implementation was defined for something which isn't a struct,
2enum, union, or trait object.
60c5eb7d
XL
3
4Erroneous code example:
5
6```compile_fail,E0118
064997fb 7impl<T> T { // error: no nominal type found for inherent implementation
60c5eb7d
XL
8 fn get_state(&self) -> String {
9 // ...
10 }
11}
12```
13
14To fix this error, please implement a trait on the type or wrap it in a struct.
15Example:
16
17```
18// we create a trait here
19trait LiveLongAndProsper {
20 fn get_state(&self) -> String;
21}
22
064997fb
FG
23// and now you can implement it on T
24impl<T> LiveLongAndProsper for T {
60c5eb7d
XL
25 fn get_state(&self) -> String {
26 "He's dead, Jim!".to_owned()
27 }
28}
29```
30
31Alternatively, you can create a newtype. A newtype is a wrapping tuple-struct.
32For example, `NewType` is a newtype over `Foo` in `struct NewType(Foo)`.
33Example:
34
35```
064997fb 36struct TypeWrapper<T>(T);
60c5eb7d 37
064997fb 38impl<T> TypeWrapper<T> {
60c5eb7d
XL
39 fn get_state(&self) -> String {
40 "Fascinating!".to_owned()
41 }
42}
43```