3 ms·
> Why can't the rust compiler introduce a hidden temporary variable for the let name = jill.name().first_name() example instead of requiring the developer to e
by Manishearth 10y ago
> Why can't the rust compiler introduce a hidden temporary variable for the let name = jill.name().first_name() example instead of requiring the developer to explicitly declare and use one?
It could, but it doesn't yet. This kind of analysis needs porting borrowck to MIR, which still has to happen. It's too complicated given the current way it works.
Though it is a matter of explicit over implicit too. If `name()` had a destructor you may want it to be explicitly tied to a scope by dint of being declared in one instead of floating around in a magical hidden variable that's tied to the scope of a different one. So there are reasons for why it may never be made to work that way.
- feanaro 10y ago> Though it is a matter of explicit over implicit too. If `name()` had a destructor you may want it to be explicitly tied to a scope by dint of being declared in one instead of floating around in a magical hidden variable that's tied to the scope of a different one. So there are reasons for why it may never be made to work that way. I'm not sure I see what the problem is with this. It seems like it is very intuitive behaviour in this case and exactly what you would expect.
- Manishearth 10y agoRust is more explicit about moving and scopes. It's currently very easy to see how long a value will live and where the destructor will run. This somewhat complicates that. This is similar to vvanders' and steve's comments in https://news.ycombinator.com/item?id=13413084 https://news.ycombinator.com/item?id=13413084 ; such things lead to a more complicated mental model even if on the surface it looks simpler.