Skip to content

Commit d2936c3

Browse files
authored
Rollup merge of #59707 - GuillaumeGomez:GuillaumeGomez-patch-1, r=Centril
Add missing tryfrom example r? @rust-lang/docs
2 parents b14662f + c386210 commit d2936c3

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
@@ -429,6 +429,26 @@ pub trait TryInto<T>: Sized {
429429
/// When the `!` type is stablized `Infallible` and `!` will be
430430
/// equivalent.
431431
///
432+
/// `TryFrom<T>` can be implemented as follows:
433+
///
434+
/// ```
435+
/// use std::convert::TryFrom;
436+
///
437+
/// struct SuperiorThanZero(i32);
438+
///
439+
/// impl TryFrom<i32> for SuperiorThanZero {
440+
/// type Error = &'static str;
441+
///
442+
/// fn try_from(value: i32) -> Result<Self, Self::Error> {
443+
/// if value < 0 {
444+
/// Err("SuperiorThanZero only accepts value superior than zero!")
445+
/// } else {
446+
/// Ok(SuperiorThanZero(value))
447+
/// }
448+
/// }
449+
/// }
450+
/// ```
451+
///
432452
/// # Examples
433453
///
434454
/// As described, [`i32`] implements `TryFrom<i64>`:

0 commit comments

Comments
 (0)