From c3862107145d245d30aaa7085aa6ade38faff8c1 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 | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/libcore/convert.rs b/src/libcore/convert.rs index 774d648558b48..82902778f3c69 100644 --- a/src/libcore/convert.rs +++ b/src/libcore/convert.rs @@ -410,6 +410,26 @@ pub trait TryInto: Sized { /// When the `!` type is stablized `Infallible` and `!` will be /// equivalent. /// +/// `TryFrom` can be implemented as follows: +/// +/// ``` +/// use std::convert::TryFrom; +/// +/// 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`: