Temporary value is freed at the end of this statement [duplicate]

This one seems convoluted at first, but remember that String and &str are different beasts.

String can live and be used on its own, but &str is just a reference to part of String. So, &str can live as long as referenced String lives. Lets see how it should work on return signatures.

let title_text = title   .text()   .trim();
//               ^       ^         ^
//               Node    String <- &str
  1. Here, title is a select::Node.

  2. Node::text returns a String, but nothing binds it to context.

  3. String::trim, in turn, returns a &str which is a reference to part of String itself.

In the end, the borrow checker just doesn’t understand how it should process a reference to String that will not live long enough in context, as it is a temporary value (non-bound).

Leave a Comment