【问题标题】:Unable to create hyper::Client because the compiler cannot infer enough type information无法创建 hyper::Client,因为编译器无法推断出足够的类型信息
【发布时间】:2016-09-07 12:12:07
【问题描述】:

为了试验 Hyper,我从 the GET example 开始。除了示例无法编译(no method `get` in `client`)这一事实之外,我将我的问题提炼为一行:

fn temp() {
    let client = Client::new();
}

此代码无法编译:

 unable to infer enough type information about `_`; type annotations or generic parameter binding required [E0282]

【问题讨论】:

  • 我自己试过了,但无法重现错误。您的文件中有extern crate hyper;use hyper::Client; 吗?这是我的工作版本:play.integer32.com/?gist=4debd4812508baf255f21715fbf44ef0
  • 将您的代码粘贴到我的 main.rs 中。同样的错误
  • 好的。当我使用 rust-lang repo 中的 hyper 时,它会编译,当我使用 hyper repo 中的 hyper={git = "github.com/hyperium/hyper"} 时,它不会编译。这也许可以解释为什么......

标签: rust type-inference hyper


【解决方案1】:

一般来说,这个错误意味着Client 有一些通用参数,编译器无法推断它的值。你必须以某种方式告诉它。

这是std::vec::Vec 的示例:

use std::vec::Vec;

fn problem() {
    let v = Vec::new(); // Problem, which Vec<???> do you want?
}

fn solution_1() {
    let mut v = Vec::<i32>::new(); // Tell the compiler directly
}

fn solution_2() {
    let mut v: Vec<i32> = Vec::new(); // Tell the compiler by specifying the type
}

fn solution_3() {
    let mut v = Vec::new();
    v.push(1); // Tell the compiler by using it
}

但是 hyper::client::Client 没有任何通用参数。您确定您要实例化的 Client 是来自 Hyper 的那个吗?

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-15
  • 2018-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多