@@ -1489,22 +1489,46 @@ For information on the design of the orphan rules, see [RFC 1023].
1489
1489
"## ,
1490
1490
1491
1491
E0118 : r##"
1492
- Rust can't find a base type for an implementation you are providing, or the type
1493
- cannot have an implementation. For example, only a named type or a trait can
1494
- have an implementation:
1492
+ You're trying to write an inherent implementation for something which isn't a
1493
+ struct nor an enum. Erroneous code example:
1495
1494
1496
1495
```
1497
- type NineString = [char, ..9] // This isn't a named type (struct, enum or trait)
1498
- impl NineString {
1499
- // Some code here
1496
+ impl (u8, u8) { // error: no base type found for inherent implementation
1497
+ fn get_state(&self) -> String {
1498
+ // ...
1499
+ }
1500
+ }
1501
+ ```
1502
+
1503
+ To fix this error, please implement a trait on the type or wrap it in a struct.
1504
+ Example:
1505
+
1506
+ ```
1507
+ // we create a trait here
1508
+ trait LiveLongAndProsper {
1509
+ fn get_state(&self) -> String;
1510
+ }
1511
+
1512
+ // and now you can implement it on (u8, u8)
1513
+ impl LiveLongAndProsper for (u8, u8) {
1514
+ fn get_state(&self) -> String {
1515
+ "He's dead, Jim!".to_owned()
1516
+ }
1500
1517
}
1501
1518
```
1502
1519
1503
- In the other, simpler case, Rust just can't find the type you are providing an
1504
- impelementation for:
1520
+ Alternatively, you can create a newtype. A newtype is a wrapping tuple-struct.
1521
+ For example, `NewType` is a newtype over `Foo` in `struct NewType(Foo)`.
1522
+ Example:
1505
1523
1506
1524
```
1507
- impl SomeTypeThatDoesntExist { }
1525
+ struct TypeWrapper((u8, u8));
1526
+
1527
+ impl TypeWrapper {
1528
+ fn get_state(&self) -> String {
1529
+ "Fascinating!".to_owned()
1530
+ }
1531
+ }
1508
1532
```
1509
1533
"## ,
1510
1534
0 commit comments