Why lifetime begins before argument calculation?


#1

Simple example of the problem:
https://play.rust-lang.org/?gist=075717100d1c911a7f207ba5f947dcdc&version=stable

Why do I need to use temporary value:
let ch = s.chars().nth(0).unwrap();
s.push(ch);

…instead of do just:
s.push(s.chars().nth(0).unwrap());
?

It seems the mutable borrow of ‘s’ occurs too early.


#2

Add #![feature(nll)] and switch to nightly to see it work.

If you use/view the function without ufcs;
String::push(&mut s, s.chars().nth(0).unwrap());
The arguments are assessed starting from the left. First one takes exclusive borrow of s, so it then can’t be used in the expression in second.
(Reverse the arguments and functions works;)

fn foo(a:char, b:&mut String) {}
foo(s.chars().nth(0).unwrap(), &mut s);