61 points · by rustyrose · 10y ago · 77 comments
I considered C, C++, Go, Rust, D, and Swift. I liked Rust and Swift the most.
Rust is a bit nicer, I admit, but Swift feels more high-level. Taken in a vacuum, it's 50-50 for me.
However, languages don't live in a vacuum. Swift has the added benefit of opening up a whole world of possibilities in the Apple ecosystem, not to mention readily available jobs. Because of this, I'd be inclined to invest in Swift rather than Rust.
My question to you is, will I miss out? Are there classes of applications that are much better suited for Rust? And I mean in practical terms, not in a theoretical, both are Turing complete way.
Because of my impression of the languages, I mentally classify Rust as lower level than Swift, and as a result I, probably mistakenly, tend to think that maybe Swift is still too high level for system programming. But performance wise they appear to be equivalent.
I want to make sure I'm not overlooking something and dismissing Rust in favor of Swift too quickly.
If you specifically want to write iOS and OSX apps, especially GUI apps, that ecosystem and its libraries melds well with Swift. It's definitely more appealing than Objective C. Swift's ecosystem seems primarily focused there, even though Swift is capable of other uses.
If you want to write cross-platform applications, services, systems, or libraries, try Rust.
This mainly had to do with the fact that the project had to be built from a hacky Rakefile, not Xcode. This was because Xcode would embed the Swift runtime only into an .app or .ipa file (because projects need to be shipped with the same Swift version used to build it, thanks to lack of ABI stability). But, as a CLI tool, there was no .app bundle - only a single executable. One downfall.
So, now that you're not using the Xcode IDE to build + run the project, you can't use the debugging IDE tooling in Xcode, making the development process that much harder. Another downfall.
I haven't checked if this has been fixed in recent builds (this all happened back in January), but figure that these are the kind of issues you're going to encounter, until the Swift language is a little more stable + proper tooling evolves.
That said, it seems pretty clear the future is Swift, so it would be better to start building experience with it sooner rather than later!
Look at this page:
https://doc.rust-lang.org/std/collections/
Not only does it explain what the data structures do, it also contains pretty solid architectural guidelines on when it makes sense to use them.
Also, Rust supports Windows (which is a high percentage of several markets), whereas Swift apparently, only unofficially (not an expert here, please correct me if I'm wrong).
I really would not pick a language that does not run effortlessly on all three major home computing platforms at this day and age unless the constraints are really well understood.
That said, Swift is a nice language, perhaps cleaner looking in syntax (although I prefer expression-oriented languages), and integrates nicely with the macOS/iOS ecosystems. I wouldn't say it's 'higher level' - Rust's upper bound on expressiveness is around the same - but it does hide away lots more of the minutia of thinking about how you manage memory.
Could you explain more? Do you mean that Swift lets you arbitrarily downcast between protocol objects?
I think Rust lets you do this too to some degree with Any. We generally stick to static bounds, though.
I'd love to see if swift does something extra, and if it would be possible to add to Rust.
For example, say we wish to write a function that builds an array from an arbitrary sequence. But as a fast path, if the sequence is already an array, just return it:
func makeArray<T:SequenceType>(from values:T) -> [T.Generator.Element] {
if let alreadyAnArray = values as? [T.Generator.Element] {
return alreadyAnArray
} else {
return Array(values)
}
}
or we can use a Golang-style technique to dispatch to a more specific protocol. See for example how debug descriptions are generated by checking conformance to a series of protocols in turn [1]: https://github.com/apple/swift/blob/1e2d192b61c9003d8b8a8593...My understanding is that this is more limited in Rust, though I'm not too familiar with how Any works - please correct if I'm wrong.
Rust & Swift are fairly similar but Swift has a much wider range of possible applications. Swift is also moving very fast (that's to be expected since it's backed by one of the most valuable companies on Earth).
This is the primary reason why I picked Swift over Rust for a project I'm working on (I'm working on an open-source password key store for high-volume services).
Can you elaborate on this? The languages seem very different to me--Swift is garbage collected (reference counting being a form of GC), for example.
ARC is not garbage collection [0]. Swift doesn't have a separate processes managing memory during the runtime. In Swift, compiler analyzes the code during the static compilation pass and inserts release/retain code during the compile time. See this SO answer for more info [1]. And you don't have to use ARC if you don't want to [2].
GC in Java or Go or CLR, for example, is completely different. Obj-C 2.0 was garbage collected but Apple hated it because of GC pauses and they vowed to remove it and never use GC again. [3] Hence, they developed ARC.
[0] https://en.wikipedia.org/wiki/Automatic_Reference_Counting
[1] http://stackoverflow.com/a/7894809
[2] https://developer.apple.com/library/ios/documentation/Swift/...
[3] https://en.wikipedia.org/wiki/Objective-C#Garbage_collection
Both Swift and Rust muddy this traditional ontology a bit. Swift's ARC has both a compile time and runtime component, and while the Rust language itself doesn't know anything about reference counting, we do provide such types in the standard library.
Plenty of GCs don't have this. You can have cycle collectors and other GCs which run the GC pass on release events (depending on some heuristic) too.
This is a terminology issue, anyway -- in academia reference counting is considered a form of GC. In software often the term is used to mean "tracing GC", or sometimes "any system that can automatically handle retain cycles".
-----
This still doesn't answer why Swift has a wider range of possible applications, though.
I do agree that Swift is or will be better suited for development on iOS, because of good API integration. Rust can program for iOS too, it's just that you will have better support for Swift :)
But aside from that, Rust should be able to apply anywhere Swift does.
Why?
The languages are very different.
Also observe that Rust's drop flag is a form of (1-bit) reference counting.
In this particular area, they are more similar than different.
Similar perhaps, but not that similar.
Rc/Arc <-> class types
Raw pointers <-> UnsafePointer/UnsafeMutablePointer
References <-> inouts
Trait objects <-> Protocol objects
Box <-> indirect enum (arguably)
Did I miss any?
Of the core language features, protocol objects are the only case where Swift uses reference counting and Rust does not.
Is it? As I understand it, memory is handled primarily by the ownership system, and sometimes by reference counting. That doesn't seem to be manual memory management to me.
Borrow checker on the other hand doesn't alter lifetimes of objects, instead it doesn't allow you to keep references to things that were destroyed. I.e. it doesn't manage memory.
Learning many languages/frameworks/styles will make you a better programmer than just investing decades into some supposed unicorn. Peter Norvig has a great post on this called Teach Yourself Programming in Ten Years. [1]
I think, because of the automatic reference counting, that your overall sense is roughly right—Swift is a little higher-level than Rust—but their overall ability to write many kinds of applications will be similar (Apple platform applications being the big exceptions for obvious reasons).
Yeah - Rust will most likely make you think more differently about programming than Swift will, and will give more scope in terms of systems programming. The advantage of Swift is that it simplifies things more, but that comes at a cost.
I mean, while Rust may make you think about ownership and memory management when programming (I have found this useful in all languages), both languages have a great type system and IMO learning to work with those is a far greater learning experience. So while Rust may teach you more, it doesn't teach you much more.
On the other hand, if you come from Haskell or something, the type systems aren't what you're looking for :)
Rust doesn't have "special integration" like Swift does with Cocoa, but it will talk to anything that will talk to C. Which is basically everything. It will talk to Java's JNI (including Android NDK), it will talk to Ruby's MRI, it will talk to CPython, it will talk to Node, and I can even integrate Rust with Swift. [It's pretty much just a matter of converting the function declarations and asking to have your project compiled into a C shared library.](https://doc.rust-lang.org/book/ffi.html) ("rust-bindgen" and "rust-cheddar" automate this)
I don't consider any of those things to be "systems programming". Any language with a GC is utterly unsuitable for systems programming. If what you're actually looking for is somewhat performant application programming, then languages like Swift and Go become an option again.
Sure, you may be capable of writing device drivers in Swift, but how much help are you going to get while doing so? How much help will you need? Will it be picked up and used by the community at large? Do any of those matter to you?
Base your language choice off the project, not vice-versa. Remember, Docker was written in Go, zookeeper in Java, and Rails (a web server as well as a framework) in Ruby.
Swift uses reference counting for all reference types (classes), but idiomatic Swift encourages using value types over reference types when possible. The chief reason to use a reference type is when you need shared ownership. Value types are not reference counted, though they may contain RC'd fields.
Rust has reference counting implemented in a library (Rc and Arc), which is what Rust programmers reach for when they need shared ownership. Rust also has a 1-bit reference count called the drop flag, which is used to dynamically determine when an object's destructor should run.
Swift's value types are less flexible than those of Rust because of the need to maintain soundness in the presence of aliasing and mutation.