let mut result = String::with_capacity(1000);
result.push_str("things... ");
result.push_str("stuff... ");
result.truncate((result.len() - 4));
However, this is a compile error. Something to do with the borrow checker and possibly mutability.
error[E0502]: cannot borrow `result` as immutable because it is also borrowed as mutable
--> <anon>:7:22
|
7 | result.truncate((result.len() - 4));
| ------ ^^^^^^ - mutable borrow ends here
| | |
| | immutable borrow occurs here
| mutable borrow occurs here
Yet, if I change it slightly I am allowed to do this:
let newlen = result.len() - 4;
result.truncate(newlen);
Why? Is there a way to change this so it can be written in one line? (P.S. this is on Rust 1.0)