“尝试!” Rust升级后,宏停止工作

时间:2015-04-23 17:02:27

标签: rust

这是一个简单的测试用例,它仍适用于playpen

use std::num;
use std::str::FromStr;
use std::convert::From;

#[derive(Debug)]
struct Error(String);

impl From<num::ParseFloatError> for Error {
    fn from(err: num::ParseFloatError) -> Error {
        Error(format!("{}", err))
    }
}

fn parse(s: &String) -> Result<f64, Error> {
    Ok(try!(<f64 as FromStr>::from_str(&s[..])))
}

fn main() {
    println!("{:?}", parse(&"10.01".to_string()));
}

然而,在我从git构建最新的rustc(现在是rustc 1.1.0-dev (1114fcd94 2015-04-23))后,它停止编译并出现以下错误:

<std macros>:6:1: 6:32 error: the trait `core::convert::From<core::num::ParseFloatError>` is not implemented for the type `Error` [E0277]
<std macros>:6 $ crate:: convert:: From:: from ( err ) ) } } )
               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<std macros>:1:1: 6:48 note: in expansion of try!
exp.rs:15:8: 15:48 note: expansion site
error: aborting due to previous error

我无法找出问题所在。为什么编译器无法找到我的特征实现?

1 个答案:

答案 0 :(得分:4)

这看起来像是一个错误:std::num::ParseFloatError<f64 as FromStr>::Err是不同的类型:

std::num正在使用后者,而std::num正在使用前者。

我打开了#24748。我还打开了#24747关于改进诊断的问题,以便将来更容易调试。

可以通过实施impl From<num::ParseFloatError> for Error的特征来解决这个问题。您需要使用<f64 as FromStr>::from_str(...)加载core::num::ParseFloatError包,并需要一些功能门。