Re:Gnirehtet rewritten in Rust
by oconnor663 on Sunday September 24, 2017 @07:10PM
(#15327009)
I think the part where they're trying to abstract out a `content` method could be made to work by adding braces, so that the `&self` borrow goes away earlier than the end of the function. I think "nonlexical lifetimes" are also going to make more cases like that work automatically without braces soon.
[ Parent
| on HN ]
> I think the part where they're trying to abstract out a `content` method could be made to work by adding braces, so that the `&self` borrow goes away earlier than the end of the function.
Not in that case. It helps when dropping a borrow earlier avoids to violate the borrowing rules, like in this case:
let mut a = [0; 20];
{
let b = &mut a[2..4];
}
let c = &mut a[6..8];
In the example from the blog post, content borrows the whole struct, and you can't drop the borrow earlier since it is used in the next line.
Re:Gnirehtet rewritten in Rust
by oconnor663 on Monday September 25, 2017 @10:22AM
(#15330705)
Right, you'd have to split up the following line so that `sum` is computed inside the block but assigned after it.
Re:Gnirehtet rewritten in Rust
by bluejekyll on Sunday September 24, 2017 @09:13PM
(#15327423)
Yes. I use that often. It’s ugly though, and lexical lifetimes should help fix this.