rust - 无法创建 hyper::Client,因为编译器无法推断出足够的类型信息

标签 rust type-inference hyper

为了试验 Hyper,我从 the GET example 开始。除了该示例无法编译这一事实之外(`client` 中没有方法 `get`),我已将问题简化为一行:

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

此代码无法编译:

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

最佳答案

一般来说,此错误意味着 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 的吗?

关于rust - 无法创建 hyper::Client,因为编译器无法推断出足够的类型信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39369650/

相关文章:

reference - CString::new().unwrap().as_ptr() 给出空的 *const c_char

collections - Vec <TO>,整数向量与String向量,为什么即使元素在堆上也可以索引/复制int元素? [复制]

vector - 将值非破坏性地附加到向量的惯用方法是什么?

rust - actix-web 处理程序中的 HTTP 请求 -> 多个执行程序一次 : EnterError

multithreading - 为什么hyper需要Handler来实现Sync,而不是每个线程使用独立的Handler?

rust - 使用 msgpack 编码时,JavaScript 对象的 Rust 等价物是什么?

typescript - 是否可以从 TypeScript 的类型推断中排除 "this"?

haskell - 为什么 Haskell 将我的 Num 类型解释为 Enum?

typescript - TypeScript 中泛型子类型的推断

rust - super 客户端无法查找在 IPv6 本地主机上运行的服务器的地址信息