gpt4 book ai didi

rust - 找不到类型 `from_str` 的名为 `hyper::mime::Mime` 的函数或关联项

转载 作者:行者123 更新时间:2023-11-29 07:58:37 26 4
gpt4 key购买 nike

我正在使用 Hyper 0.11,它可以重新导出箱子“mime”。我正在尝试从字符串创建 MIME 类型:

extern crate hyper;

fn main() {
let test1 = hyper::mime::Mime::from_str("text/html+xml").unwrap();
}

错误:

error[E0599]: no function or associated item named `from_str` found for type `hyper::<unnamed>::Mime` in the current scope

为什么会出现这个错误,是什么原因造成的?如何解决?

最佳答案

你得到这个错误是因为,果然,Mime 上没有定义from_str 方法。 .为了解析像 Mime::from_str 这样的名称,from_str 必须是 Mime 的固有方法(不是特征的一部分) ,或者在使用它的地方范围内的特征的一部分。 FromStr 不在范围内,因此您会收到错误消息——尽管错误消息会告诉您出了什么问题以及如何解决:

error[E0599]: no function or associated item named `from_str` found for type `hyper::<unnamed>::Mime` in the current scope
--> src/main.rs:3:13
|
3 | let test1 = hyper::mime::Mime::from_str("text/html+xml").unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: items from traits can only be used if the trait is in scope
= note: the following trait is implemented but not in scope, perhaps add a `use` for it:
candidate #1: `use std::str::FromStr;`

但是,在这种特殊情况下,更常见的做法是使用FromStr,而不是直接调用from_str,而是通过parse 间接调用from_strstr 上的方法,如 FromStr's documentation提及。

let test1: hyper::mime::Mime = "text/html+xml".parse().unwrap();

关于rust - 找不到类型 `from_str` 的名为 `hyper::mime::Mime` 的函数或关联项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48655891/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com