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
-
Here,
title
is aselect::Node
. -
Node::text
returns aString
, but nothing binds it to context. -
String::trim
, in turn, returns a&str
which is a reference to part ofString
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).