Is there any practical difference between:
var n: int
for i in 0..100:
n = i
and
for i in 0..100:
let n = i
In practice i'm making multiple http requests and am unsure if assigning each request body to a single var is better or worse then assigning each request body to a new let.
In your first example you're reassigning the value to the same, already initialized variable each iteration, while in the second you're declaring a new variable each iteration and it will get dropped (stops existing) at the end of it. You'll understand the difference as soon as you'll try to use n for "doing stuff" outside of the loop.
Understanding scopes can be not enough to fully understand what your program will do. Personally, I can honestly say that I don't know what the compiler is going to do in these particular isolated cases. It will probably optimize both those loops away since they are completely side-effect free. When I really need to understand these nuances use godbolt.org or read the C code generated.
In your third and fourth examples I'd say obeying the intent of your code is more important that any technical considerations unless you're at the optimization stage. Reassigning a string is probably better from the performance point of view since it will decrease the number of memory allocations happening.
But again, I suggest following the logic: if there's no way you need both responses at the same time (i.e. the logic is linear), there's no point in dropping the first variable and declaring the second one with the same name. In practice, however, those two responses probably have some contextual meaning which is usually better to keep reflected in the variable names.
I'm no computer scientist but if I understand it correctly let will allocate a new location on the stack every time while var will use the same memory location unless a larger memory block is needed in which case a new memory block is allocated?
I'm not either, but I think it's mostly correct. However, strings (and seqs, which strings really are) are heap allocated.
Also, I'm not sure how much memory a seq that was initialized without specifying the required capacity (newSeqOfCap or newStringOfCap) will allocate. It probably doesn't, but it could allocate more than the actual length of your first response, in which case it could happen that there won't be any reallocation happening at the second assignment. BTW, where in the docs can one read about seq allocation rules?
Yeah, that's a very nice article and it confirms at least this point: