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

前端 未结 1 1708
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-20 03:15

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 =          


        
1条回答
  •  野的像风
    2021-01-20 03:26

    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::::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;`
    

    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();
    

    0 讨论(0)
提交回复
热议问题