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?
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With