如何解决“需要的注释类型:无法解析_”何时调用泛型静态方法? [英] How to resolve "type annotations required: cannot resolve _" when calling generic static method?

查看:203
本文介绍了如何解决“需要的注释类型:无法解析_”何时调用泛型静态方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在不同的静态方法中调用泛型静态方法,但是我得到一个令人困惑的错误:

 错误:输入需要注解:无法解析`_:Config` [--explain E0283] 
- > src / main.rs:15:38
|>
15 |> json=>返回Config :: parse_json ::< T>(source,datatype),
|> ^^^^^^^^^^^^^^^^^^^^^^^
注意:需要`Config :: parse_json`

当我运行 rustc --explain E0283 时,错误信息显示:


当编译器没有足够的信息
来明确地选择实现时会发生此错误。


$
$ b

使用rustc_serialize:b
$ b

使用rustc_serialize:b
$ b

使用rustc_serialize: :JSON;
使用std :: fs :: File;
使用std :: io :: prelude :: *;
使用std :: path :: PathBuf;
使用rustc_serialize;

pub trait配置{
fn get_config< T:rustc_serialize :: Decodable>(来源:PathBuf,数据类型:T) - >选项< T> {
let extension = source.extension()。unwrap();
如果让Some(extension)= extension.to_str(){
匹配扩展名{
json=>返回Config :: parse_json ::< T>(source,datatype),
_ => (无法解析扩展名),
}
} else {
panic!(没有找到扩展名);
}
}

fn parse_json< T:rustc_serialize :: Decodable>(source:PathBuf,datatype:T) - >选项< T> {
let mut file = File :: open(source).unwrap();
let mut contents = String :: new();
file.read_to_string(& mut contents).unwrap();
让解码:T = json :: decode(& contents).unwrap();
让选项:选项< T> =一些(数据类型);
返回选项;
}
}


解决方案

它意味着Rust无法确定 T 的类型。 Rust通用方法通过为您实际在代码中使用的每个具体 T 生成一个单独的实现。



您可以使用类似的方法解决它:

 返回Config :: parse_json(源,数据类型为AConcreteDataType); 

但是为了确切地知道问题,我们需要查看其余的调用代码 main.rs



除此之外, parse_json 方法看起来iffy;为什么它返回数据类型而不是解码结果?


I am trying to call a generic static method within a different static method, but I get a confusing error:

error: type annotations required: cannot resolve `_: Config` [--explain E0283]
  --> src/main.rs:15:38
   |>
15 |>                     "json" => return Config::parse_json::<T>(source, datatype),
   |>                                      ^^^^^^^^^^^^^^^^^^^^^^^
note: required by `Config::parse_json`

When I ran rustc --explain E0283, the error message said:

This error occurs when the compiler doesn't have enough information to unambiguously choose an implementation.

Which is confusing as there is only one implementation of the function.

use rustc_serialize::json;
use std::fs::File;
use std::io::prelude::*;
use std::path::PathBuf;
use rustc_serialize;

pub trait Config {
    fn get_config<T: rustc_serialize::Decodable>(source: PathBuf, datatype: T) -> Option<T> {
        let extension = source.extension().unwrap();
        if let Some(extension) = extension.to_str() {
            match extension {
                "json" => return Config::parse_json::<T>(source, datatype),
                _ => panic!("Unable to parse the specfied extension."),
            }
        } else {
            panic!("No extension was found.");
        }
    }

    fn parse_json<T: rustc_serialize::Decodable>(source: PathBuf, datatype: T) -> Option<T> {
        let mut file = File::open(source).unwrap();
        let mut contents = String::new();
        file.read_to_string(&mut contents).unwrap();
        let decoded: T = json::decode(&contents).unwrap();
        let option: Option<T> = Some(datatype);
        return option;
    }
}

解决方案

It means Rust couldn't figure out the type of T. Rust generic methods work by generating a separate implementation for each concrete T that you actually use in your code. Meaning you need a concrete type spelled out somewhere.

You could fix it by using something like:

return Config::parse_json(source, datatype as AConcreteDataType);

But to know the problem for sure, we'd need to see the rest of the calling code in main.rs.

Aside from that, the parse_json method looks iffy; why is it returning the datatype instead of the decoded result?

这篇关于如何解决“需要的注释类型:无法解析_”何时调用泛型静态方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆