let mut result = String::with_capacity(1000);
result.push_str("things... ");
result.push_str("stuff... ");
result.truncate((result.len() - 4));
但是,这是一个编译错误。与借用检查器和可能的可变性有关。
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
然而,如果我稍微改变它,我可以这样做:
let newlen = result.len() - 4;
result.truncate(newlen);
为什么?有没有办法改变它,所以它可以写在一行中?(PS 这是在 Rust 1.0 上)