找不到类型为 `hyper::mime::Mime` 的名为 `from_str` 的函数或关联项 [英] no function or associated item named `from_str` found for type `hyper::mime::Mime`
问题描述
我正在使用 Hyper 0.11,它会重新导出板条箱mime".我正在尝试从字符串创建 MIME 类型:
I'm using Hyper 0.11 which re-exports the crate "mime". I'm trying to create a MIME type from a string:
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
为什么会出现这个错误,原因是什么?如何解决?
Why is this error, what's the cause? How to fix it?
推荐答案
你得到这个错误是因为,果然,没有在 Mime
.为了解析像 Mime::from_str
这样的名称,from_str
必须是 Mime
的固有方法(不是特征的一部分),或在使用它的地方范围内的特征的一部分.FromStr
不在范围内,因此您会收到错误消息——尽管错误消息甚至会告诉您出了什么问题以及如何解决:
You get this error because, sure enough, there is no from_str
method defined on Mime
. In order to resolve a name like Mime::from_str
, from_str
has to be either an inherent method of Mime
(not part of a trait), or part of a trait that is in scope in the place where it's used. FromStr
isn't in scope, so you get an error -- although the error message goes so far as to tell you what's wrong and how to fix it:
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
str 上的方法
,作为 FromStr
的文档 提及.
However, in this particular case, it's more common to use FromStr
not by calling from_str
directly, but indirectly with the parse
method on str
, as FromStr
's documentation mentions.
let test1: hyper::mime::Mime = "text/html+xml".parse().unwrap();
这篇关于找不到类型为 `hyper::mime::Mime` 的名为 `from_str` 的函数或关联项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!