From 645d95ead195398e32cc1a57c38d68000340b962 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 29 Mar 2019 23:22:11 +0100 Subject: [PATCH] Add missing tryfrom example --- src/libcore/convert.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/libcore/convert.rs b/src/libcore/convert.rs index 774d648558b48..060c07b496d3e 100644 --- a/src/libcore/convert.rs +++ b/src/libcore/convert.rs @@ -410,6 +410,24 @@ pub trait TryInto: Sized { /// When the `!` type is stablized `Infallible` and `!` will be /// equivalent. /// +/// It can be implemented as follows: +/// +/// ``` +/// struct SuperiorThanZero(i32); +/// +/// impl TryFrom for SuperiorThanZero { +/// type Error = &'static str; +/// +/// fn try_from(value: i32) -> Result { +/// if value < 0 { +/// Err("SuperiorThanZero only accepts value superior than zero!") +/// } else { +/// Ok(SuperiorThanZero(value)) +/// } +/// } +/// } +/// ``` +/// /// # Examples /// /// As described, [`i32`] implements `TryFrom`: