4 ms·
I find that borrow checked in Rust is the easiest part of the language to get used to. Lifetimes on the other hand seem unnecessary in 99% cases where compiler
by dmz73 3y ago
I find that borrow checked in Rust is the easiest part of the language to get used to.
Lifetimes on the other hand seem unnecessary in 99% cases where compiler forces you to use them - why can't it just assume that all the required parts have the 'a and complain if something violates that?
Event bigger issue is all the missing features that most other languages have - overloaded functions (they kind of exist in generics), ranged integers, arrays indexed by enums, full support for type aliases, some level of inheritance, even if it was the go style, would make Rust much much easier to use.
- samatman 3y ago> Lifetimes on the other hand seem unnecessary in 99% cases where compiler forces you to use them - why can't it just assume that all the required parts have the 'a and complain if something violates that? Rust does that, this is lifetime elision. If the compiler complains about missing lifetime annotations, that means it can't infer the lifetime. Granted that it not-infrequently feels like it should be able to, and there's definitely room for improvement on its lifetime inference. It's conservative, meaning that an elided lifetime will always be valid, but some lifetimes which could be elided get missed by inference and need to be added anyway. But most of the times that I've yelled at the compiler for complaining about missing lifetimes, it was right and I was wrong.
- jpc0 3y agoTo the contrary I ran into cases pretty quickly that rust just wasn't useful for in my opinion. One of my use cases was something like a mutable borrow of an struct in a struct in a single threaded code path wasn't allowed because the entire struct was mutably borrowed or some nonsense. I believe the "idiomatic" way was to create some weird abstraction to appease the borrow checker that the thing being borrowed once is only being borrowed once. I rewrote the code in C++ and never looked back.
- lenkite 3y agoFrom what I have observed, most advanced Rust authors don't bother with lifetimes at all. Every pointer becomes an integer index into some slice and they mostly skip the lifetime/borrow checking. Lifetime are only kept on the few, easy-to-reason about cases. Kind of funny actually.
- duped 3y agoForbidding multiple mutable borrows is how Rust is able to prevent you from writing code with bugs like iterator invalidation. Has nothing to do with threading.