diff --git a/src/libcore/convert.rs b/src/libcore/convert.rs index 2d999868f71ec..d9501de7fe8e2 100644 --- a/src/libcore/convert.rs +++ b/src/libcore/convert.rs @@ -216,6 +216,13 @@ impl From for T { fn from(t: T) -> T { t } } +// A (the) value of the unit type can be created from any value of any type. +// This is useful for use with the `try!` macro or the `?` operator. +#[stable(feature = "unit_from_anything", since = "1.10.0")] +impl From for () { + fn from(t: T) -> () {} +} + //////////////////////////////////////////////////////////////////////////////// // CONCRETE IMPLS //////////////////////////////////////////////////////////////////////////////// diff --git a/src/libcoretest/tuple.rs b/src/libcoretest/tuple.rs index 4fe5e0a740bf7..7bcd0863183ed 100644 --- a/src/libcoretest/tuple.rs +++ b/src/libcoretest/tuple.rs @@ -66,3 +66,13 @@ fn test_show() { let s = format!("{:?}", (1, "hi", true)); assert_eq!(s, "(1, \"hi\", true)"); } + +#[test] +fn test_convert_unit_from_any() { + fn io() -> Result { Ok(1) } + fn fmt() -> Result { Ok(2) } + fn sum() -> Result { + Ok(io()? + try!(fmt())) + } + assert_eq!(sum(), Ok(3)) +}