Add basic Unstable Book entry for `on_unimplemented`.

This commit is contained in:
Corey Farwell 2017-05-20 09:38:45 -04:00
parent 0bd9e1f5e6
commit 0c97d6c855
1 changed files with 37 additions and 0 deletions

View File

@ -6,5 +6,42 @@ The tracking issue for this feature is: [#29628]
------------------------
The `on_unimplemented` feature provides the `#[rustc_on_unimplemented]`
attribute, which allows trait definitions to add specialized notes to error
messages when an implementation was expected but not found.
For example:
```rust,compile_fail
#![feature(on_unimplemented)]
#[rustc_on_unimplemented="a collection of type `{Self}` cannot be built from an \
iterator over elements of type `{A}`"]
trait MyIterator<A> {
fn next(&mut self) -> A;
}
fn iterate_chars<I: MyIterator<char>>(i: I) {
// ...
}
fn main() {
iterate_chars(&[1, 2, 3][..]);
}
```
When the user compiles this, they will see the following;
```txt
error[E0277]: the trait bound `&[{integer}]: MyIterator<char>` is not satisfied
--> <anon>:14:5
|
14 | iterate_chars(&[1, 2, 3][..]);
| ^^^^^^^^^^^^^ the trait `MyIterator<char>` is not implemented for `&[{integer}]`
|
= note: a collection of type `&[{integer}]` cannot be built from an iterator over elements of type `char`
= note: required by `iterate_chars`
error: aborting due to previous error
```