5 ms·
It'll never not have a trade-off! But there are some things that can make it easier in many cases: - Copy structs- ownership issues vanish when things can be b
by brundolf 3y ago
It'll never not have a trade-off! But there are some things that can make it easier in many cases:
- Copy structs- ownership issues vanish when things can be bitwise-copied
- .clone()- don't be afraid to clone outside of hot loops! If allocations are a problem, look at using Rc instead of Box where applicable
- Pure functions/methods- you don't need mutable references if you aren't mutating things
- Macros- reduce boilerplate
- Purpose-built traits- you can hang methods off of different structs, including std or builtin structs, that can make certain flows really ergonomic
In general: Rust asks you to care about all the little details at a language level, but it also empowers you in different ways to build high-level abstractions on top of those details so that you often don't have to care about them. I'm finding that I have the smoothest time with it when I use traits and macros to make my own little DSL for the problem I'm trying to solve, and then write my business logic in that.
And then- many libraries offer you their own abstractions that give you the same sort of benefit. Concurrency normally involves a bunch of ownership and locking headaches, but something like rayon makes it breezy by giving you an abstraction that handles those details for you. Async web route handlers with shared state from scratch would normally be hard, but axum and Rocket give you abstractions that handle the gross stuff for you. One of my favorite crates I recently discovered is just called "memoize", and it just gives you a macro you can slap on any old function to memoize its results! (global mutable state!) https://crates.io/crates/memoize https://crates.io/crates/memoize. This would be a mess to implement yourself, but the abstraction makes it breezy
And then personally: the main reason I'd use a Rust library like the OP over a Python library is the tooling/dependency management. Running a Rust project with dependencies Just Works and that's extremely valuable to me. But, it'll always be weighed against the costs of using a lower-level language.
- bombela 3y agoAnd the implementation behind those abstractions tend to be of really good quality. Thanks to the borrow checker and the scrutiny around the unsafe keyword. In other words I am more willing to trust abstractions over tricky problems in Rust than I would in other languages. Just for a second, try to imagine a generic memoize in C++.
- brundolf 3y agoYep, exactly. The implementation - and more importantly, the contract - are much more dependable. For better and worse Rust is a language that leans all-in on abstraction, going to great lengths to make those as safe and scalable as possible, and if you aren't using them you're going to have a harder time than you need to
- yaroslavyar 3y agoMaybe I’m a bit ate for the party. But I hope to get a suggestion for switching to the ext language in the real life. Like I’m quite proficient with swift language, I’m making money with it for years, so I consider my proficient comes not just from general computer science knowledge, but it comes from the getting o know the environment as well. Like I known the sdt lib, I know common caveats with the project, I have a pretty much experience it’s it after all. And so I’ve tried rust. I mean it’s fantastic, it have awesome tooling, concise syntax and well crafted design, but I barely could write script with it. And I’m seeing clearly that this will lasts for a year or something, and even then I’ll suck at it towards an average senior dev focused on it a way beefier me. So I really can’t crack it myself, like how do you folks switching to a new lang to get paid for it?
- brundolf 3y agoI've been using Rust for about five years, but I've never been paid for it and I'm not sure I ever will be. I just like it! Personally I don't think it's worth trying to predict which technologies will be the most valuable and learning them based on that, unless you're struggling to make money right now. It sounds like Swift is working great as a money-maker for you right now, so I recommend just following your interests outside of that! Maybe they'll lead to another money-maker, maybe they won't and that's okay I don't see Rust eating any domains outside of low-level systems development anytime soon, so I wouldn't worry that you're going to get left behind or something. It's not going to make Swift, or JavaScript, or Python, or Java, or Go, or many other things obsolete