繁体   English   中英

找不到类型为hyper :: mime :: Mime`的名为from_str的函数或相关项目

[英]no function or associated item named `from_str` found for type `hyper::mime::Mime`

我正在使用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

为什么会出现此错误,原因是什么? 如何解决?

之所以会出现此错误,是因为,毫无from_strMime上没有定义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 ,而是通过strparse方法间接调用from_str ,就像FromStr的文档中提到的那样。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM