Skip to content

Commit 0c97d6c

Browse files
committed
Add basic Unstable Book entry for on_unimplemented.
1 parent 0bd9e1f commit 0c97d6c

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

src/doc/unstable-book/src/language-features/on-unimplemented.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,42 @@ The tracking issue for this feature is: [#29628]
66

77
------------------------
88

9+
The `on_unimplemented` feature provides the `#[rustc_on_unimplemented]`
10+
attribute, which allows trait definitions to add specialized notes to error
11+
messages when an implementation was expected but not found.
912

13+
For example:
14+
15+
```rust,compile_fail
16+
#![feature(on_unimplemented)]
17+
18+
#[rustc_on_unimplemented="a collection of type `{Self}` cannot be built from an \
19+
iterator over elements of type `{A}`"]
20+
trait MyIterator<A> {
21+
fn next(&mut self) -> A;
22+
}
23+
24+
fn iterate_chars<I: MyIterator<char>>(i: I) {
25+
// ...
26+
}
27+
28+
fn main() {
29+
iterate_chars(&[1, 2, 3][..]);
30+
}
31+
```
32+
33+
When the user compiles this, they will see the following;
34+
35+
```txt
36+
error[E0277]: the trait bound `&[{integer}]: MyIterator<char>` is not satisfied
37+
--> <anon>:14:5
38+
|
39+
14 | iterate_chars(&[1, 2, 3][..]);
40+
| ^^^^^^^^^^^^^ the trait `MyIterator<char>` is not implemented for `&[{integer}]`
41+
|
42+
= note: a collection of type `&[{integer}]` cannot be built from an iterator over elements of type `char`
43+
= note: required by `iterate_chars`
44+
45+
error: aborting due to previous error
46+
```
1047

0 commit comments

Comments
 (0)