Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

rust

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

The error:

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?

like image 299
Olegu Avatar asked Feb 07 '18 04:02

Olegu


1 Answers

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

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();
like image 122
trent Avatar answered Nov 15 '22 09:11

trent