The statement selected_branch = msg.split(" ").nth(2).unwrap(); throws the following compiler error: "Borrowed value does not live long enough
I assume the error means that the result of msg.split(" ").nth(2).unwrap(); is borrowed by selected_branch - is this the case?
Anyway, how do I solve this problem?
The problem is that the selected_branch variable lives longer than msg, so it can't contain a reference to msg. (I was a little surprised by this, since I wasn’t familiar with how ordering of function parameters affects their scopes.)
One solution is to store the reference in a new variable with a shorter scope. You can give the new variable the same name and it will “shadow” the original: