Why lifetime begins before argument calculation?

Simple example of the problem:

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

…instead of do just:

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

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);
1 Like