// Copyright 2016 The Rust Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution and at // http://rust-lang.org/COPYRIGHT. // // Licensed under the Apache License, Version 2.0 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. use std::num::{ParseFloatError, ParseIntError}; fn main() { assert_eq!(simple(), Ok(1)); assert_eq!(nested(), Ok(2)); assert_eq!(merge_ok(), Ok(3.0)); assert_eq!(merge_int_err(), Err(Error::Int)); assert_eq!(merge_float_err(), Err(Error::Float)); } fn simple() -> Result { Ok(try!("1".parse())) } fn nested() -> Result { Ok(try!(try!("2".parse::()).to_string().parse::())) } fn merge_ok() -> Result { Ok(try!("1".parse::()) as f32 + try!("2.0".parse::())) } fn merge_int_err() -> Result { Ok(try!("a".parse::()) as f32 + try!("2.0".parse::())) } fn merge_float_err() -> Result { Ok(try!("1".parse::()) as f32 + try!("b".parse::())) } #[derive(Debug, PartialEq)] enum Error { Int, Float, } impl From for Error { fn from(_: ParseIntError) -> Error { Error::Int } } impl From for Error { fn from(_: ParseFloatError) -> Error { Error::Float } }