Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

The awkwardness is because use of `self.whatever` in closures borrows `self` as a whole instead of just the `whatever` part. The good news is that Rust is going to fix this soon:

https://blog.rust-lang.org/2021/05/11/edition-2021.html#disj...



Oh thank god. This has been so annoying in the past


Thank you so much! that's very interesting.


If the problem is as pornel described, you can work around it by creating the borrow of self.whatever outside the closure.

Instead of:

    foo(|| {
        bar(&self.whatever);
    });
... do:

    let whatever = &self.whatever;
    foo(|| {
        bar(whatever);
    });
To avoid leaking `whatever` into the scope, there's an idiom that looks like this:

    foo({
        let whatever = &self.whatever;
        || {
            bar(whatever);
        }
    });


Yeah that's what I've had to do, it just seemed like an oversight tbh. Maybe the idea is to not keep the entire GUI state in a single object though, but it's the one time learning rust that I thought "hey that doesn't make sense". which really speaks to Rust's strengths, rather than its weaknesses. Can't wait for 2021




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: