]> git.proxmox.com Git - rustc.git/blob - src/doc/unstable-book/src/language-features/on-unimplemented.md
New upstream version 1.23.0+dfsg1
[rustc.git] / src / doc / unstable-book / src / language-features / on-unimplemented.md
1 # `on_unimplemented`
2
3 The tracking issue for this feature is: [#29628]
4
5 [#29628]: https://github.com/rust-lang/rust/issues/29628
6
7 ------------------------
8
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.
12
13 For example:
14
15 ```rust,compile_fail
16 #![feature(on_unimplemented)]
17
18 #[rustc_on_unimplemented="an iterator over elements of type `{A}` \
19 cannot be built from a collection of type `{Self}`"]
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 | ^^^^^^^^^^^^^ an iterator over elements of type `char` cannot be built from a collection of type `&[{integer}]`
41 |
42 = help: the trait `MyIterator<char>` is not implemented for `&[{integer}]`
43 = note: required by `iterate_chars`
44
45 error: aborting due to previous error
46 ```
47