Skip to content

Commit 645d95e

Browse files
Add missing tryfrom example
1 parent 4c27fb1 commit 645d95e

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/libcore/convert.rs

+18
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,24 @@ pub trait TryInto<T>: Sized {
410410
/// When the `!` type is stablized `Infallible` and `!` will be
411411
/// equivalent.
412412
///
413+
/// It can be implemented as follows:
414+
///
415+
/// ```
416+
/// struct SuperiorThanZero(i32);
417+
///
418+
/// impl TryFrom<i32> for SuperiorThanZero {
419+
/// type Error = &'static str;
420+
///
421+
/// fn try_from(value: i32) -> Result<Self, Self::Error> {
422+
/// if value < 0 {
423+
/// Err("SuperiorThanZero only accepts value superior than zero!")
424+
/// } else {
425+
/// Ok(SuperiorThanZero(value))
426+
/// }
427+
/// }
428+
/// }
429+
/// ```
430+
///
413431
/// # Examples
414432
///
415433
/// As described, [`i32`] implements `TryFrom<i64>`:

0 commit comments

Comments
 (0)