Love you all guys for awesome responses
, they offer a lot of details and insights into this topic and I've learnt a lot of new things thru-out these days! (I also realized how the official book and Rustonomicon need a lot more updates to incorporate these information actually, I din't find answers in there or maybe because of my ignorance
).
The thing that did click into my mind and help unblocking my thought process is this paragraph:
The main problem with your original example
fn main () {
let mut i:i32 = 1;
let j = {
let x = &mut i;
let y = &x;
&**y
};
}
is that the variable x only exists inside the block, hence the reference to x cannot be alive after that block.
So I consider @steffahn 's answer is the solution! It enabled me to understand the other answers of @quinedot and @alice (sorry I'm so noob
).
Deep dived into this topic, I found another thread in which @steffahn explained in very detailed way about re-borrowing, I would consider it to be "The unofficial guide on re-borrowing", and encourage fellow users to read thru it. Lifetime and borrow are hard to fully acquire so it's definitely worth to invest your time guys. In that thread, @quinedot also provided other 2 articles which are super helpful and informative:
I also found a similar question on this thread, and I hope the answers here could help you as well @zylthinking .
Last but not least, I was hopeless because I couldn't find any answer to my question before, and my last option was signing up on this user page and immediately creating this thread. This community has amazed me
, @alice responded in less than 1 hour, followed by @quinedot @steffahn a few hours after that. You guys did not only answer the question with awesome details and demo code (I couldn't think of anyone on the internet could spend that much of time to try my examples, not even count to create a demo to help me to understand
), but also replied to my follow-up questions as well as provided further resources for me to learn more.
This is phenomenal I would say. So I would like to thank you all and encourage you guys to continue keeping this up like what you guys have been doing so far
, this community needs people like you! ![]()
(I'll go grinding your other answers on other topics, so don't be surprised if you receive
sometimes
).