These are chat archives for rust-lang/rust

4th
May 2017
qthree
@qthree
May 04 2017 08:22
Hello. Explain please what's wrong with this code: https://is.gd/o2GsDY
Simplified version: https://is.gd/ES2SMX
Frank McSherry
@frankmcsherry
May 04 2017 08:27
Rust's borrowing is currently based on lexical scopes. For Rust to be happy the scopes need to be disjoint (whereas in your second example, one contains the other.
That being said my "this will obviously fix it" fix didn't work. https://is.gd/EHT9VB
qthree
@qthree
May 04 2017 08:29
That's the point. It's something weird with lifetimes in function declaration.
Frank McSherry
@frankmcsherry
May 04 2017 08:31
My best non-expert guess is that the rules for lifetime reasoning require the loop's body to be unified as a lifetime. Are you interested in equivalent code that does work, or to understand more about why it does what it does? (( lots of the lifetime complaints should be interpreted as "It is too hard to prove that your current code does the right thing; perhaps you should change it?" ))
Daniel Collin
@emoon
May 04 2017 08:33
I guess (?) it might be related to this rust-lang/rfcs#811
Frank McSherry
@frankmcsherry
May 04 2017 08:34
Ah, I see what you are saying. Yes you have said that the resulting borrow has exactly the same lifetime as the input reference, which you've sneaked into the middle of the loop. My guess (about to test) writing the loop to do a thing and then exit, and then returning the reference, should work better.
Right, so this seems to work great, and I think is equivalent to your first program: https://is.gd/5ix4Qn
(( Maybe swap a != 10 in for a < 10 if the intent is to wrap around for input values greater than 9. ))