&&str.to_owned error

Hi all!
I'm confused. Please see demo Rust Playground.
I want to get String from &&str, but i can't. Why?
Thank you!

Given that Rust Playground works (and is probably what you should write here, imho) I am assuming it has to do with inference/deref coercions, but I am not sure why.

I think it's because ToOwned is implemented for all types that are Clone, which includes references.

1 Like

It appears it's the fact that the &str is being coerced into a String so the extra reference is getting in the way.

fn main() {
    let test = String::from("abc");
    let test_ref = &test;
    let test_ref_ref = &test_ref;
    
    let clone = test_ref_ref.to_owned();
    
    println!("{:?}", clone);
}

This code works. I'm guessing it's because there is no coercion.

It compiles, but it does not do what the OP wants (which is creating an owned string). println! doesn't care about that :slight_smile:

1 Like