Skip to content

Commit c386210

Browse files
Add missing tryfrom example
1 parent 4c27fb1 commit c386210

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/libcore/convert.rs

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

0 commit comments

Comments
 (0)