this case
fn main() {
let mut a = String::from("abc");
let mut b = &mut a;// 'a
let c = &mut b; // 'b
*b = String::from("123");
println!("{}", c);// 'a and 'b extend here
}
and the error is
why *b
is borrowed?
this case
fn main() {
let mut a = String::from("abc");
let mut b = &mut a;// 'a
let c = &mut b; // 'b
*b = String::from("123");
println!("{}", c);// 'a and 'b extend here
}
and the error is
why *b
is borrowed?
c
is an exclusive borrow of b
, of type &'b mut &'a mut String
, through which you can do any of these things:
"abc"
String
owned by a
with another oneb
points toSo, by the general principles of exclusive borrowing in Rust, you cannot use b
while it is borrowed by c
. (The fact that you then only use c
for reading, not writing, is irrelevant.)
Is this reborrow?
I wouldn't say so, because c
isn't pointing to any part of what b
points to; it is simply a reference to b
. But it's certainly a borrow derived from b
in some way, which just like a reborrow constrains how you can use b
(in this case, completely blocks using b
).
It's a nested borrow, not a reborrow.
b
can't be used while c
is active, and that includes writing to *b
.
If you just meant why is the error phrased that way, as far as I know, it's just repeating the place being written to. But regardless, every reference b
is borrowing the pointed-to place, *b
.
To understand lifetime, I recommend this article.
If you are a Chinese, this I wrote in Chinese may help.
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.