3

Consider this statement: From<T> for U implies Into<U> for T. source

let t="abc"; and note that t has type &str

Everyone has used: let s=String::from(t);

So that we have: From<&str> for String

According to the implication above we have:Into<String> for &str

However, the following does not work:

fn main(){
    let z="abc";
    let x = String::from(z);
    let s=&str::into(x);
}

What am I not understanding?

5
  • 2
    Note that the existence of String::from does not imply that String implements the trait From.
    – jthulhu
    Aug 25 at 15:55
  • Do you mean let s = &str::into(z);? Aug 25 at 15:59
  • Why do you believe "everyone is using String::from()"? I for one use .to_owned(), and I know plenty that use .to_string() or .into() (but <&str>::into() is rather uncommon, because why when you can use method syntax instead?) Aug 26 at 18:40
  • @jthulhu Well, I'd say it would be unusual if it wouldn't. Aug 26 at 18:41
  • @ChayimFriedman my point is that, as the OP may not know, implementing a trait does not work like interfaces (or duck typing) might work in other languages, where having the same methods as required in a trait is enough to implement the trait. My point was not that types that have a from associated function do not usually do so because they implement the From trait.
    – jthulhu
    Aug 26 at 19:11

2 Answers 2

8

It works, except that &str::into(z) is understood as &(str::into(z)). If you want to call the associated function into for type &str, you must use a qualified name for that type:

fn main() {
    let z = "abc";
    let x = String::from(z);
    let s: String = <&str>::into(z);
}

Note that the type annotation String was added because otherwise Rust cannot know which type is the target of into.

Otherwise, you can also use the method syntax

    let s: String = z.into();

or the associated function of trait Into:

    let s: String = Into::into(z);
5
  • Why is &str::into(z) understood as &(str::into(z)) ?
    – user22447547
    Aug 25 at 15:58
  • @Brett Its just the way operator precedence was designed. From the reference, paths (i.e. ::) has a higher precedence than unary operators (i.e. &). That and you can only use :: with identifiers (which &str is not, but str is) unless you do <...>::.
    – kmdreko
    Aug 25 at 16:04
  • @Brett additionally, & cannot be a part of a path. If you need to name a type that involves something other than an identifier, you need to wrap it with < and >.
    – jthulhu
    Aug 25 at 16:05
  • 1
    @jthulhu your main() does not compile.
    – user22447547
    Aug 25 at 16:18
  • @Brett Looks like a simple typo (they used x where they should've used z). I've corrected it.
    – cdhowie
    Aug 25 at 16:37
2

You got turned around there a bit. Happens to me a lot with the whole to and from ;)

Let's consider the types. x is String. So into(x) would attempt to turn a String into a &str, and you can't do that like this.

The fact that String implements From<&str> means that &str implements Into<String>, i.e., you can turn a &str into a String.

Here's what you really want:

fn main() {
    let x: &str = "abc";
    let z: String = String::from(x);
    
    let s: String = x.into();
}

No problem there!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.