4 ms·
The compiler also loves to tell you that your borrows aren't long-lived enough and you need to make them `'static`, when making them `'static` is almost always
by Arnavion 5y ago
The compiler also loves to tell you that your borrows aren't long-lived enough and you need to make them `'static`, when making them `'static` is almost always the opposite of what you actually do to resolve the problem - change the borrow to be a move of the original value or a move of an Rc/Arc of the original value.
I've seen multiple colleagues new to Rust ask me for help with their code that has `&'static mut`, and when I tell them that's never going to work and ask them how they got there, it's always because the compiler told them to do it.
Like [1] says, you eventually get the experience to ignore the compiler error message (or at least the "suggestions"), and instead understand the real problem and apply the real fix yourself.
Similarly, when you have a case of value of type A being assigned to a binding of type B, there's a 50% chance that the compiler gets the "expected type" and "actual type" the other way around from what you want. This isn't really a fixable problem, because only you the human know which one you expected and which one you didn't. But it's yet another case where you eventually gain the experience of ignoring the "expected type" "actual type" parts of the compiler error because they don't matter, and just looking at the types.
[1]: https://news.ycombinator.com/item?id=28014288 https://news.ycombinator.com/item?id=28014288
- estebank 5y ago> The compiler also loves to tell you that your borrows aren't long-lived enough and you need to make them `'static`, when making them `'static` is almost always the opposite of what you actually do to resolve the problem Most of the time the compiler complains about an unmet 'static lifetime, what it's actually talking about is about wanting an owned type. Sadly, I haven't gotten around to making those diagnostics more accurate (from the user's points of view). > I've seen multiple colleagues new to Rust ask me for help with their code that has `&'static mut`, and when I tell them that's never going to work and ask them how they got there, it's always because the compiler told them to do it. > Like [1] says, you eventually get the experience to ignore the compiler error message (or at least the "suggestions"), and instead understand the real problem and apply the real fix yourself. I would appreciate bug reports at https://github.com/rust-lang/rust/issues/ https://github.com/rust-lang/rust/issues/ when encountering these kind of situations. If there's something worse than missing suggestions is inaccurate or misleading suggestions.