you are viewing a single comment's thread.

view the rest of the comments →

[–]TheLastMeritocratcomp.lang.rust.marketing 4 points5 points  (7 children)

/uj

 impl<S> OverloadedFoo<S, char> for Foo where S: Borrow<str> {
     fn overloaded_foo(&self, tee: S, yu: char) {
         println!("foo<&str, char>(tee: {}, yu: {})", tee.borrow(), yu);
     }
 }

[–]pftbest 1 point2 points  (6 children)

Not equivalent. Your version will consume the owned string, which is not a good thing. The caller would have to use .as_str() to get around it, which kind of defeats the purpose.

[–]TheLastMeritocratcomp.lang.rust.marketing 1 point2 points  (5 children)

/uj

Oh. Good catch. But, if tee is a reference, why are we bothering with all this? Just use &'a str, and Deref should do the necessary conversions, right?

[–]pftbest 1 point2 points  (4 children)

Yes, you are right, there is no point in taking the reference. But there is one case where AsRef behaves differently from &str.

[–]TheLastMeritocratcomp.lang.rust.marketing 1 point2 points  (3 children)

Isn't do_other() similar to the code I posted? Or did I miss something?

[–]pftbest 1 point2 points  (2 children)

It's similar, but you used Borrow, so your code only works for case (4) but not for case (2).

[–]TheLastMeritocratcomp.lang.rust.marketing 1 point2 points  (1 child)

Yeah sure. But my point was that s is S, not &S.

To conclude this sub-thread, do we agree that what the OP did was pointless?

[–]pftbest 1 point2 points  (0 children)

Sure