2

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?

Peter Hall
  • 53,120
  • 14
  • 139
  • 204
SK.Chen
  • 113
  • 1
  • 1
  • 6
  • I asked a concise Question for this common rust error `error[E0716]` [error E0716: temporary value dropped while borrowed (rust)](https://stackoverflow.com/questions/71626083/error-e0716-temporary-value-dropped-while-borrowed-rust) – JamesThomasMoon Mar 26 '22 at 07:23

0 Answers0