about summary refs log tree commit diff
path: root/src/libcore
diff options
context:
space:
mode:
authorMazdak Farrokhzad <twingoow@gmail.com>2019-04-06 00:14:46 +0200
committerGitHub <noreply@github.com>2019-04-06 00:14:46 +0200
commitc065367ca05b291844a753c987538e24683aa90a (patch)
tree2be9dab604a783ab4b53027d5dfe00eb761d22fb /src/libcore
parentf3f68987197c384f44ea6bb96dc88caa4ade193a (diff)
parentc3862107145d245d30aaa7085aa6ade38faff8c1 (diff)
downloadrust-c065367ca05b291844a753c987538e24683aa90a.tar.gz
rust-c065367ca05b291844a753c987538e24683aa90a.zip
Rollup merge of #59707 - GuillaumeGomez:GuillaumeGomez-patch-1, r=Centril
Add missing tryfrom example

r? @rust-lang/docs
Diffstat (limited to 'src/libcore')
-rw-r--r--src/libcore/convert.rs20
1 files changed, 20 insertions, 0 deletions
diff --git a/src/libcore/convert.rs b/src/libcore/convert.rs
index 7b9e19e36a2..e903bd936c4 100644
--- a/src/libcore/convert.rs
+++ b/src/libcore/convert.rs
@@ -429,6 +429,26 @@ pub trait TryInto<T>: Sized {
 /// When the `!` type is stablized `Infallible` and `!` will be
 /// equivalent.
 ///
+/// `TryFrom<T>` can be implemented as follows:
+///
+/// ```
+/// use std::convert::TryFrom;
+///
+/// struct SuperiorThanZero(i32);
+///
+/// impl TryFrom<i32> for SuperiorThanZero {
+///     type Error = &'static str;
+///
+///     fn try_from(value: i32) -> Result<Self, Self::Error> {
+///         if value < 0 {
+///             Err("SuperiorThanZero only accepts value superior than zero!")
+///         } else {
+///             Ok(SuperiorThanZero(value))
+///         }
+///     }
+/// }
+/// ```
+///
 /// # Examples
 ///
 /// As described, [`i32`] implements `TryFrom<i64>`: