Skip to content

Commit

Permalink
Rollup merge of rust-lang#59707 - GuillaumeGomez:GuillaumeGomez-patch…
Browse files Browse the repository at this point in the history
…-1, r=Centril

Add missing tryfrom example

r? @rust-lang/docs
  • Loading branch information
Centril committed Apr 5, 2019
2 parents b14662f + c386210 commit d2936c3
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/libcore/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>`:
Expand Down

0 comments on commit d2936c3

Please sign in to comment.