Now, at some point I want to highlight the latest line with something like this:
...
let mut term = terminal_setup();
line_highlight(&mut term, term.h);
This is failing with error[E0503]: cannot use term.h because it was mutably borrowed. If this makes sense from the point of view of the borrow checker I fail to understand why this is a problem for at least two reasons:
I can still do something like the following but it is ugly:
let foo = term.h;
line_highlight(&mut term, foo);
Since my function is accepting u16, shouldn't the value of term.h just be copied into the local line variable (parameter) when the function is called? No pointer / heap is involved here, so why is this a problem?
Yes, I get that. That's why I said that the error makes sense from the borrow checker point of view but I was probably expecting the compiler to be a bit smarter than that since the h value is being copied into the line variable at function-call time. But I see that since it is desugaring before the function call this is causing the problem.
There is one case where the compiler is smarter: If the function is a method (has a self parameter), then two-phase borrows allow later arguments to access the struct without invalidating the borrow of the self argument. For example, this works: