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