How to implement Into <MyType> for & str

I have a custom pub struct Foo type, and I would like to say that strings can be converted to Foo types. I am trying to make impl<'a> Into<Foo> for &'a str , but I know from this answer that I cannot do this. What other options do I have?

In context, I'm trying to do something like

 trait Foo { type ArgType; fn new<I: Into<Self::ArgType>>(arg: I) -> Self; } struct MyType; impl Into<MyType> for str { fn into(self) -> MyType { MyType } } struct Bar; impl Foo for Bar { type ArgType = MyType; fn new<I: Into<MyType>>(arg: I) -> Bar { Bar } } 
+6
source share
1 answer

As Chris Morgan noted, FromStr is an option. From<&str> will be different. The latter would give you a built-in implementation of Into<Foo> for &str too (because there is a hidden meaning of Into<U> For T where U: From<T> ).

+4
source

All Articles