`std::convert::From<mime::Mime>` is implemented for `&mut str`, but not for `&str`

Hi Guys,

I implemented From<&'static str> for the enum Mime, but I get a error when call the .into() method:

#[derive(PartialEq, Debug)]
pub enum Mime{
    Other(&'static str),

impl From<&'static str> for Mime {
    fn from(s: &'static str) -> Mime {

        match s {

            "text/plain" => Mime::Plain,

            "application/json" => Mime::JSON,

            "text/html" => Mime::HTML,

            "application/javascript" => Mime::JavaScript,

            "image/png" => Mime::PNG,

            "application/octet-stream" => Mime::OctetStream,

            _ => Mime::Other(s),





mod tests{

    use super::Mime;


    fn test_mime(){

        let m = "text/plain";

        assert_eq!(Mime::Plain, Mime::from(m));

        let actual: &'static str = Mime::Plain.into();

        assert_eq!(m, actual);



I got this error:

error[E0277]: the trait bound &str: std::convert::From<mime::Mime> is not satisfied
--> src\mime.rs:33:48
33 | let actual: &'static str = Mime::Plain.into();
| ^^^^ the trait std::convert::From<mime::Mime> is not implemented for &str
= note: std::convert::From<mime::Mime> is implemented for &mut str, but not for &str
= note: required because of the requirements on the impl of std::convert::Into<&str> for mime::Mime

Thanks for help.

You've implemented From<&str> for Mime (the &str -> Mime direction) but your trying to use the Mime -> &str direction.

1 Like

Please follow the instructions for this forum on how to format your code in posts: Forum Code Formatting and Syntax Highlighting. You can use the edit button below your initial post to correct its formatting.

1 Like

Thanks, from https://doc.rust-lang.org/std/convert/trait.Into.html:

One should avoid implementing Into and implement From instead. Implementing From automatically provides one with an implementation of Into thanks to the blanket implementation in the standard library.

That's true in the &str -> Mime direction. &str: From<Mime> provides Mime: Into<&str>. You could do let mine: Mime = "text/plain".into() and it would work.

For the opposite direction to work, you need to implement a conversion in that direction, namely, From<Mime> for &str.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.