I'm confused by this error message about the type &str
:
let a = &String::from("abcdefg"); // ok!
let a = String::from("abcdefg").as_str(); // compile error
The error:
let a = String::from("abcdefg").as_str();
^^^^^^^^^^^^^^^^^^^^^^^ - temporary value dropped here while still borrowed
temporary value does not live long enough
I understand that in the second line, the String
object is a temporary object, it drops when the line is over. But why does the first line run ok?