【发布时间】:2016-08-23 14:41:34
【问题描述】:
我试图在不同的静态方法中调用通用静态方法,但我得到一个令人困惑的错误:
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`
当我运行rustc --explain E0283时,错误信息说:
当编译器没有足够的信息时会发生此错误 明确地选择一个实现。
这很令人困惑,因为该函数只有一种实现。
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;
}
}
【问题讨论】:
-
您没有在任何地方使用
self或Self;为什么这些功能在一个特征中?错误可能是没有 nothing 将方法与实现类型联系起来。 -
我不需要 self 做它是一个静态方法,但你是对的,不被某些东西所暗示......显然我需要更多的咖啡。不过还是谢谢。