我有一个关于所有权的新手问题,我试图在最后一个字节上更新(+ = 1)并打印出UTF-8字符。 但是我有可变的借位给String,以便更改最后一个字节,因此我无法打印(使用不可变的借位)。 Rustacean这样做的方式是什么?
注意:我知道我做的不好,谢谢!
fn main() {
let s = vec![240, 159, 140, 145];
let mut s = unsafe {
String::from_utf8_unchecked(s)
};
unsafe {
let bytes = s.as_bytes_mut(); // mutable borrow occurs here
for _ in 0..7 {
println!("{}", s); // Crash here as immutable borrow occurs here
bytes[3] += 1;
}
}
println!("{}", s);
}