Navigation Menu

Skip to content

Commit

Permalink
Add missing tryfrom example
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Apr 5, 2019
1 parent 4c27fb1 commit c386210
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/libcore/convert.rs
Expand Up @@ -410,6 +410,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>`:
Expand Down

0 comments on commit c386210

Please sign in to comment.