&&str.to_owned error


#1

Hi all!
I’m confused. Please see demo http://is.gd/cgVedg.
I want to get String from &&str, but i can’t. Why?
Thank you!


#2

Given that http://is.gd/O88f0s 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.


#3

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


#4

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.


#5

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: