From 0c97d6c85550d795407a5c7cbaf6d1553c338b9d Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Sat, 20 May 2017 09:38:45 -0400 Subject: [PATCH] Add basic Unstable Book entry for `on_unimplemented`. --- .../src/language-features/on-unimplemented.md | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/doc/unstable-book/src/language-features/on-unimplemented.md b/src/doc/unstable-book/src/language-features/on-unimplemented.md index 81f284d0a6a..9eea3fccbbc 100644 --- a/src/doc/unstable-book/src/language-features/on-unimplemented.md +++ b/src/doc/unstable-book/src/language-features/on-unimplemented.md @@ -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 { + fn next(&mut self) -> A; +} + +fn iterate_chars>(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` is not satisfied + --> :14:5 + | +14 | iterate_chars(&[1, 2, 3][..]); + | ^^^^^^^^^^^^^ the trait `MyIterator` 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 +``` -- 2.44.0