I’ve been programming for decades, though usually for myself, not as a profession. My current go-to language is Python, but I’m thinking of learning either Swift (I’m currently on the Apple ecosystem), or Rust. Which one do you think will be the best in terms of machine learning support in a couple of years and how easy is it to build MacOS/ iOS apps on Rust?
Rust is the only language I know of that is actively being used at the kernel level all the way through to the web app level. Compare that with Swift which is not only mostly tied to a single ecosystem, but even the “cross platform” stuff like libdispatch is littered with code like:
if #available(macOS 10.12, iOS 10.0, tvOS 10.0, watchOS 3.0, *)
Note libdispatch runs on older versions of Apple Platforms than those version numbers. The backwards compatible code paths aren’t just for other operating systems - that’s how it works on older Apple platforms too.
deleted by creator
Swift has little to no use outside the apple ecosystem, and even if you are currently using Apple, you have to consider your targets as well. Writing in Swift means your code will only be usable by other Apple users, which is canonically a rather small fraction of technology users. Rust on the other hand is multiplatform and super low level, there’s very few other languages out there that can match the potential of applications of rust code. Thus you will, in time, be introduced to many other technologies as well, like AI/ML, low level programming, web, integrations between languages, IoT, those are only a few of all the possibilities. On the other hand, even if Swift has a much more mature ecosystem, it’s still only good for creating UIs in all things Apple, which is pretty telling; Apple is not willing to put in the time and effort to open it’s language to other fields, because it sees no value in them being the ones providing the tooling for other purposes. They pretty much only want people to code web apps for them, and Swift delivers just fine for that. So if your current purpose is making Apple UIs, you could learn Swift, but be warned that either you’ll either be doing that your whole life or will eventually be forced to change languages again.
Then again, most languages nowadays aren’t that different from each other. I can code in a truckload of languages, not because I actually spent time making something coherent and complete with each one of them, but because I know some underlying concepts that all programming languages follow, like OOP, or functional programming, and whatever those entail. If you learn those you will not be afraid to switch languages on a whim, because you’ll know you can get familiar with any of them within a day.
Rust on the other hand is multiplatform and super low level
Not to nitpick here, (I agree with pretty much everything you said) but I wouldn’t go around calling Rust super low level as it is garbage collected. The borrow checker acts as a abstraction over the actual malloc and free calls that are happening under the hood.
I think you don’t know what garbage collection is. Allocations and Deallocations is how the heap works in memory, and is one of the two main structures in it, the stack being the other one. No matter what language you are using, you cannot escape the heap, except if you don’t use a modern multitasking OS. ARC is a type of garbage collection that decides when to free a reference after it is allocated (malloc), by counting how many places refer to it. When it reaches 0, it frees the memory (free). With ARC you don’t know when a reference will be freed on compile time.
In Rust, the compiler makes sure, using the Borrow checker, that there is only one place in your entire program where a reference can be freed, so that it can insert the free call at that place AT COMPILE TIME. That way, when the program runs there is no need for a garbage collection scheme or algorithm to take care of freeing up unused resources in the heap. Maybe you thought the borrow checker runs at compile time, taking care of your references, but that’s not the case, the borrow checker is a static analysis phase in the Rust compiler (rustc). If you want to use a runtime borrow checker, it exists, it’s called RefCell, but it’s not endorsed to use. Plus, when you use RefCell, you also usually use Reference Counting (Rc RefCell)
Perhaps garbage collection is the wrong term to use as it dosen’t happen at runtime (I wasn’t sure what other term to call what Rust does). But Rust does provide a abstraction over manual manual memory management and if you are experienced with Rust sure you can probably visualize where the compiler would put the malloc and free calls so it is kind of a mix where you do technically have control it is just hidden from you.
Edit: It seems the term is just compile-time garbage collection so maybe you could consider it falling under garbage collection as an umbrella term.
Isn’t that basically the same as how C++ RAII works?
Essentially although there are a few key differences:
- In Rust there always only one owner while in C++ you can leak ownership if you are using shared_ptr.
- In Rust you can borrow references you do not own safely and in C++ there is no gurantee a unique_ptr can be shared safely.
- In Rust, A lot more compile time optimization for the borrow checker is available whereas in C++ the type system dosen’t always let the compiler know for sure when an object goes out of scope, is moved, or is destroyed and so you miss out on a lot of optimization that would be trivial with Rust like syntax.
You raised an issue that the other bulletpoint has the solution for, I really don’t see how these are “key differences”.
In Rust there always only one owner while in C++ you can leak ownership if you are using shared_ptr.
That’s what unique_ptr would be for. If you don’t want to leak ownership, unique pointer is exactly what you are looking for.
In Rust you can borrow references you do not own safely and in C++ there is no gurantee a unique_ptr can be shared safely.
Well yeah, because that’s what shared_ptr is for. If you need to borrow references, then it’s a shared lifetime. If the code doesn’t participate in lifetime, then ofcourse you can pass a reference safely even to whatever a unique_ptr points to.
The last bulletpoint, sure that’s a key difference, but it’s partially incorrect. I deal with performance (as well as write Rust code professionally), this set of optimizations isn’t so impactful in an average large codebase. There’s no magical optimization that can be done to improve how fast objects get destroyed, but what you can optimize is aliasing issues, which languages like C++ and C have issues with (which is why vendor specific keywords like
__restrict
exists). This can have profound impact in very small segments of your codebase, though the average programmer is rarely ever going to run into that case.
Pretty much, with some atomic additions like “you cannot mutate a reference when it is borrowed immutably elsewhere” or “you cannot borrow a reference mutably multiple times”.
deleted by creator
Other than having first class support on Apple’s hardware Swift dosen’t have much going for it. There is no killer feature in Swift, it dosen’t widespread features and it only has a small niche. If you want to develop for mainly Apple devices I would say go for it as that is the niche it was designed for. Although I see from your post you want to do ML, Python for the high level stuff + C++ for the low level stuff is probably your best pick for that. May I ask what type of ML are you going for? Are you mainly using libraries like Tensorflow, Pytorch etc… or are you into the nitty gritty of building these things yourself and writing the required code for the matrix math and training algorithms.
Swift is a nice language though.
But I’m obviously on team Rust^^ for various reasons (one being that you can do the whole stack in Rust (not that it’s necessarily the best choice for each level, but it really composes well and with a little bit of trait-magic abstraction in the higher levels it works quite well IME)
For ML, python yes, certainly for high-level stuff at least currently. I wouldn’t be so sure in the future about the lower stack though, Rust seems to gain momentum there as well (potentially replacing use-cases where currently python is dominant too).
I think ML is probably going to require a lot of people in the future and I’m looking to build a digital nomad skill set for the future that pays well. While I’ve done a postgrad subject on ML and have a STEM degree, but I’m inclined to use existing libraries as that’s just easier.
If you want to train your neural nets you can maybe check out: https://github.com/rust-ml/linfa https://github.com/param087/swiftML (Rust seems to have more active support in terms of libraries)
If you want to integrate ML into an IOS/MacOs app: https://developer.apple.com/documentation/coreml
For userland apps Swift would be better and for training or just being generally being more useful in the future go for Rust.
At the end of the day just choose the language that is more enjoyable for you.
Sensational answer! Thank you.
There’s a recent Rust ML framework called “burn”. So maybe there’s also a future for ML in Rust for you.
I think rust is good for learning some low level concepts, especially coming from python.
I don’t think Python is going anywhere in the ML space though.
Agree. I’m kinda looking for marketable skills though and I feel Python may be becoming saturated.
A programming language itself isn’t a marketable skill!
Learn the underlying concepts of programming and how computers work and you’ll be able to move from language/framework to pretty much any language/framework easily.
Language absolutely is a marketable skill because most companies are looking to hire someone who can start working day one not someone they’ll have to train for weeks or even months in a new language that heavily relies on some specific framework.
I have to disagree. I’ve been conducting interviews for a fairly large software shop (~2000 engineers) for about 3 years now and, unless I’m doing an intern or very entry level interview, I don’t care what language they use (both personally and from a company interviewer policy), as long as they can show me they understand the principles behind the interview question (usually the design of a small file system or web app)
Most devs with a good understanding of underlying principles will be able to start working on meaningful tasks in a number of days.
It’s the candidates who spent their time deep diving into a specific tool or framework (like leaving a rails/react boot camp or something) that have the hardest time adjusting to new tools.
Plus when your language/framework falls out of favor, you’re left without much recourse.
If you’re trying to prepare for a couple of years in advance, it might be worth spending a day playing with each language just to see which one feels best to you. Both languages should be able to do anything you want but some things will probably be more difficult in one or the other. I’ve never used swift, but I know rust can have a rather steep learning curve. That may be deterrent enough for some people, but that’s up to you to decide if that struggle is worth it.
Thanks, this makes some sense. I’ve started a few tutorials for Swift, and I added the Rust plugin/module to Visual Studio Code, but neither felt intuitive to me.
That doesn’t surprise me too much. They’re both a good bit different than python. It’s okay to take a little more time with each of them. Maybe try building one simple thing in both for more of a 1-1 comparison.
deleted by creator
@Bluetreefrog
I, like you, code for myself not others and not professionally. Take a dive into Xcode and Swift if you’re in the Apple world. It is just stupid easy to throw together an app or tool in no time at all.Have you played with the Swift ML frameworks at all?
If you don’t have a Mac I don’t think you can get the MacOS SDK.
So in that case I’d recommend Rust. I still think most of Rust’s tools/frameworks need more time in the oven but Rust is massive and has tools being built for everything. If you want Mobile I’d recommend you take a look at Dioxus or Tauri. There are probably others as well but I don’t know them it’s been a while since I’ve looked.
Pascal
Lol, Turbo Pascal was the first OO language I learned, back before there was any such thing as an Internet… Showing my age now.
Julia
I have thought about Julia.
What are your thoughts on it?
Julia looks like it is pointed towards ML programming and is fast, but I don’t see the same level of potential in a few years that Rust and Swift seem to have.
Rust seems to generating a lot more buzz and I’ve been seeing posts about Swifts ML libraries that look interesting. My crystal ball seems to be saying that Rust will follow a similar arc that Python took and gain some serious ML creds through libraries built by community/industry. I think Swift will also gain some credible ML capabilities too because it has the Apple behemoth behind it.
Something to consider as well is learning both. Swift is certainly the best choice for making macOS/iOS GUIs. Other languages are probably better than Swift for your ML needs (could be rust, Python, etc.). However it’s totally possible to have an app using multiple languages. You could have the UI portion be in Swift, but the ML portions be in another language.
At my company we have a Mac app with the GUI written in Swift, shared logic with our Windows app written in C++, and some libraries written in Rust. So it’s certainly possible.
One caveat is that some languages don’t work with each other very well. Swift and Python do work well together iirc, so doing UI code in Swift and ML code in Python may not be a bad idea.
If you want to just stick to Swift, Apple does have some ML frameworks for Swift that you can use. I don’t do any work with ML, so I have no idea if these frameworks are any good, or have good resources for learning.
If you want to just stick with whatever language you use for ML, there are GUI libraries in nearly every language. These certainly won’t be as robust or as nice to work with as the native frameworks in Swift, but they could probably get the job done. I do know that a major issue with GUIs in Python is the difficulty in multi threading, which is a must for any app that performs long tasks without the UI freezing.
Just learn whatever you currently need. If you know a few paradigms, learning a new language of the same paradigm is easy-peasy and can be done rather quickly (well at least being be productive with it, doing stuff idiomatically often takes a little bit longer).
That said, Rust IMO is a language that makes sense to learn anyway, since it also teaches you to program in a nicer way (not just true for Rust, there are other languages that have this effect as well, such as Haskell etc. generally languages that introduce something really new (i.e. a new paradigm)). Generally it makes sense to learn multiple languages, as each brings you new ideas. But on the other hand it makes sense to learn one language really well (I’d recommend that being Rust, as it can cover so many use-cases and is generally designed nicely (it fills a sweet spot between mutability and functional programming IMHO).