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

I'll need to play around with that. I just started doing a serious dive into Rust about a month ago.


Tip for you: when the compiler says you need a 'static lifetime, it's actually trying to say that all temporary references are forbidden and won't work. Arc is a reference, but isn't temporary. Add a mutex or atomic when you need to modify data behind arc.

This is the "interior mutability" approach that the article mentions.


This is very useful, so useful that I can't help but think that the compiler actually should say what you said.


I definitely think the error could be improved but I'm not sure it should really say "just shove it in an `Arc<Mutex>`," because that really is only sometimes the right solution and it might lead people to make poor choices by default.

I think a lot of the problem is really with the confusingness of the concept of static lifetime in rust to begin with, where it's kinda used for both "lives forever" and "doesn't reference anything that lives longer than it"[1]. I hope someday these meanings get different names, tbh.

But when you see that error the naive thought is like... "ok better make it live forever" but it really just means you need to make it something that you control, and there are other ways to do that than a refcounting mutex.

[1] https://doc.rust-lang.org/rust-by-example/scope/lifetime/sta...


That's very useful, thanks! I need to learn more Rust.


That's very useful, thanks!


Try raising an issue on GitHub. Rust people are interested in improvements like that. I'm not saying it will be accepted, but it won't be ignored.




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

Search: