Swift 6.0 Adopts Rust-Inspired Features: What Do Rustaceans Think?
Swift 6.0, expected to be released in September 2024, will introduce several new features that appear to be inspired by concepts from Rust. Many of these features focus on enhancing concurrency in Swift, drawing parallels to Rust’s strong emphasis on safety and ownership. Let's explore some of these changes and see how they compare to what Rustaceans are already familiar with.
Sendable: Swift’s Take on Safe Concurrency
One of the key concepts in Swift is the Sendable protocol. Similar to Rust’s Send and Sync traits, Sendable is used to ensure that a type can be safely transferred between concurrent tasks without risking data races or other concurrency issues. Although the Sendable protocol isn’t new in Swift 6.0, it's worth mentioning here as it lays the groundwork for the stricter concurrency rules that are coming in this release.
Stricter Rules for Sharing Types Across Tasks
In Swift 6.0, there will be tighter restrictions on how types can be shared between concurrent tasks. This new approach is reminiscent of Rust’s borrowing system. By default, Sendable types can be shared freely across tasks. However, not all types are Sendable—sometimes making a type Sendable is too costly or impractical. Despite this, there are cases where you might still need to share non-Sendable types between tasks.
To address this, Swift 6.0 introduces region-based isolation. This features help determine whether a value has been moved out, similar to how Rust handles borrowing and ownership. Compiler will perform region-based flow-sensitive checking to ensure that non-Sendable types can be safely sent across isolation boundaries. Additionally, the new sending keyword allows developers to explicitly mark when a value is being moved out, adding clarity and safety to concurrent code.
For those interested in the technical details, here are links to the official proposals:
The Growing Influence of Ownership and Borrowing
It’s not surprising to see concepts like ownership and borrowing gaining traction in languages beyond Rust. I'd love to hear what Rustaceans think about Swift’s adoption of these ideas. Do you believe that Swift is moving in the right direction by embracing Rust-inspired features?
(Maybe I forgot to mention other features that are inspired by Rust, but I think these are the most significant ones I think. If you have any other features in mind, please let me know.)