<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Rhymu</title>
    <description>The latest articles on Forem by Rhymu (@rhymu).</description>
    <link>https://forem.com/rhymu</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F303793%2F6917cfff-dbd1-4ff6-af0c-50855aa07165.jpg</url>
      <title>Forem: Rhymu</title>
      <link>https://forem.com/rhymu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/rhymu"/>
    <language>en</language>
    <item>
      <title>Swift vs. Rust -- an Overview of Swift from a Rusty Perspective</title>
      <dc:creator>Rhymu</dc:creator>
      <pubDate>Sun, 07 Feb 2021 04:50:36 +0000</pubDate>
      <link>https://forem.com/rhymu/swift-vs-rust-an-overview-of-swift-from-a-rusty-perspective-18c7</link>
      <guid>https://forem.com/rhymu/swift-vs-rust-an-overview-of-swift-from-a-rusty-perspective-18c7</guid>
      <description>&lt;p&gt;I first learned Swift in 2017, which for me was ages ago.  Back then it was at version 3.1.  I'm much more familiar with Rust, having been studying and using it now for about 8 months.  So when I went back to check out Swift again this week, I was really surprised by three things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Swift is now version 5.3 (two major versions!)&lt;/li&gt;
&lt;li&gt;Swift has finally been ported to Windows!&lt;/li&gt;
&lt;li&gt;Wow, Swift code sure looks a lot like Rust code!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I felt it might be fun to sit down and take notes while reading through the official &lt;a href="https://docs.swift.org/swift-book/"&gt;Swift Book&lt;/a&gt; to "freshen up" on the language.  Being very much into Rust, I thought it would also be fun to include in my notes any comparisons I could draw between Swift and Rust.  It took a few days, and by the end I had quite a few notes written down.  So I decided to clean them up a bit and post them here!&lt;/p&gt;

&lt;p&gt;The following is essentially a boiled-down comparison of Swift (version 5.3) with respect to Rust (edition 2018).  It follows along the chapters of the official &lt;a href="https://docs.swift.org/swift-book/"&gt;Swift Book&lt;/a&gt;.  Swift and Rust are very similar, so in general, if a specific point of comparison isn't mentioned here, it may be that the two languages are the same on that point, or "close enough" that I felt it didn't matter.  For example, &lt;code&gt;true&lt;/code&gt; and &lt;code&gt;false&lt;/code&gt; are the same for both Swift and Rust, and the syntax for various kinds of string literals are largely the same, so I didn't bother to write them down.  On the other hand, something not listed here could be something I didn't consider or know about at the time, in which case please let me know in the comments.  Thank you in advance!&lt;/p&gt;

&lt;p&gt;If any information below is not completely verified or tested, a question-mark &lt;code&gt;?&lt;/code&gt; punctuation will be used.  This is an open invitation to anyone who can verify the information (whether correct or incorrect) and provide evidence one way or another.&lt;/p&gt;

&lt;p&gt;First of all, I found a few general topics of interest that weren't covered directly by the &lt;a href="https://docs.swift.org/swift-book/"&gt;Swift Book&lt;/a&gt;.  I grouped them together to list here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://swift.org/package-manager/"&gt;Swift Package Manager&lt;/a&gt; — analogous to &lt;a href="https://doc.rust-lang.org/book/ch01-03-hello-cargo.html"&gt;cargo&lt;/a&gt; in Rust, being a standard package management system for the language.

&lt;ul&gt;
&lt;li&gt;Note that unfortunately, as of the time of this writing, the Swift Package Manager is not available for Windows, so ad-hoc solutions based on tools such as CMake and Ninja are often used to construct build systems for Swift projects on Windows instead.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://developer.apple.com/documentation/swift/swift_standard_library"&gt;Swift Standard Library&lt;/a&gt; — this is tightly coupled with the compiler and facilitates the runtime and various types and functions essential to various aspects of the language.  It also provides support for features not covered by the book:

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://developer.apple.com/documentation/swift/swift_standard_library/encoding_decoding_and_serialization"&gt;Serialization&lt;/a&gt; — a set of protocols analogous to &lt;a href="https://serde.rs/"&gt;serde&lt;/a&gt;:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Encodable&lt;/code&gt; — &lt;code&gt;serde::Serialize&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Decodable&lt;/code&gt; — &lt;code&gt;serde::Deserialize&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Encoder&lt;/code&gt; — &lt;code&gt;serde::Serializer&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Decoder&lt;/code&gt; — &lt;code&gt;serde::Deserializer&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://developer.apple.com/documentation/swift/swift_standard_library/c_interoperability"&gt;C Interoperability&lt;/a&gt; — various types and functions analogous to Rust's Foreign Function Interface (FFI).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://developer.apple.com/documentation/foundation"&gt;Foundation&lt;/a&gt; — this library originates from the Apple ecosystem (iOS, macOS, etc.) and is typically bundled with Swift due to providing access to essential operating system services, which in Rust are typically either built into the Rust standard library or are available from the Rust community's crate registry:

&lt;ul&gt;
&lt;li&gt;Date and time management&lt;/li&gt;
&lt;li&gt;Localization (formatting of locale-aware string representations)&lt;/li&gt;
&lt;li&gt;File system — creating, reading, writing, or examining files and directories&lt;/li&gt;
&lt;li&gt;JSON encoding/decoding&lt;/li&gt;
&lt;li&gt;Processes and threads&lt;/li&gt;
&lt;li&gt;Networking&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://developer.apple.com/documentation/Dispatch"&gt;Dispatch&lt;/a&gt; (also known as "Grand Central Dispatch" (GCD) — this library originates from the Apple ecosystem (iOS, macOS, etc.) and is typically bundled with Swift due to providing facilities for asynchronous programming, analogous to futures/async in Rust along with &lt;code&gt;futures-rs&lt;/code&gt; and runtimes such as &lt;code&gt;tokio&lt;/code&gt;, &lt;code&gt;async-std&lt;/code&gt;, and &lt;code&gt;smol&lt;/code&gt; available from the Rust community's crate registry.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  TL;DR — Overall Differences
&lt;/h2&gt;

&lt;p&gt;I wrote quite a few notes down here, perhaps too many for some people.  So here's a summary of "what's important" when comparing Swift and Rust:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Swift broadly categorizes types into "value" and "reference" types, hiding most of the implementation details of references from the programmer.  Rust makes references a first-class type and opens the entire type system up for the programmer, for better or worse.&lt;/li&gt;
&lt;li&gt;Swift supports class inheritance, while Rust does not.&lt;/li&gt;
&lt;li&gt;Swift uses the throwing and catching of exceptions for error handling, while Rust encourages the use of enumerated types to return "success or failure" variants from fallible operations.&lt;/li&gt;
&lt;li&gt;Most code constructs in Rust are expressions, whereas Swift has a more traditional separation between "statement" and "expression".&lt;/li&gt;
&lt;li&gt;Unlike Rust, Swift offers a comprehensive "properties" feature set (i.e. computed properties, property observers, projected values) similar to C#.&lt;/li&gt;
&lt;li&gt;Many language features that have their own keywords and syntax in Swift are handled through Rust's type system (i.e. optional types, boxing, references, operator overloading).&lt;/li&gt;
&lt;li&gt;Rust includes direct syntax for asynchronous programming (&lt;code&gt;async&lt;/code&gt;, &lt;code&gt;await&lt;/code&gt;), while Swift does not, relying on adjunct library facilities to support it.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Opinion Section
&lt;/h3&gt;

&lt;p&gt;I've tried to remain as objective as possible while writing this post.  However, there were a few subjective things I felt during this process.  Skip to the next section unless you're comfortable with a bit of biased opinions.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Both languages aim to be general purpose systems programming languges. However, Swift has solid Apple roots and thus suffers from two main weaknesses:

&lt;ul&gt;
&lt;li&gt;Although Swift has strong corporate (Apple) backing, its overall community is weak in comparison to Rust.  Being relatively unencumbered, Rust has a more "well-rounded" base and robust community, as evidenced by:

&lt;ul&gt;
&lt;li&gt;Wider range of target platforms which support it.&lt;/li&gt;
&lt;li&gt;Easier procedure to obtain, set up, and operate its toolchain.&lt;/li&gt;
&lt;li&gt;Richer online environment for publishing and reusing libraries.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;While Rust includes more advanced features such as threading, networking, and asynchronous coding directly in its standard library, Swift leverages adjunct libraries such as Foundation and GCD which were designed for Apple's ecosystem specifically.  Supporting other platforms entails "porting" these libraries after the fact or risking "crippling" the language for platforms which don't support the Apple libraries.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Rust and Swift both aim to be "safe", referring to language design and compiler features which prevent and/or detect errors at compile-time.  However, they have fundamentally different approaches:

&lt;ul&gt;
&lt;li&gt;Rust has invested in the "borrow checker" approach where references are first-class types and the concept of "lifetimes" rises to the conscious level of the programmer.

&lt;ul&gt;
&lt;li&gt;Cost: more difficult for the programmer to learn, due to this approach being non-traditional and the syntax being more elaborate, requiring more practice and different ways of thinking about and designing code.&lt;/li&gt;
&lt;li&gt;Gain: faster, more efficient code which can leverage both the stack and heap, allowing more sophisticated zero-copy designs and optimizations.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Swift took a traditional "pass by copy or reference" approach, with references being inherently reference-counted.

&lt;ul&gt;
&lt;li&gt;Gain: very similar to traditional programming languages, with semantics already familiar to most programmers, making the language easier to learn and use.&lt;/li&gt;
&lt;li&gt;Cost: since the more traditional approach is taken, many of the more traditional drawbacks remain, such as unnecessary copying and indirection; although arguably the compiler has opportunities to optimize at least some of these away.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;In the realm of Object-Oriented Programming, Rust takes a heavy-handed approach encouraging the &lt;a href="https://en.wikipedia.org/wiki/Composition_over_inheritance"&gt;Composition over inheritance&lt;/a&gt; principle by not supporting inheritance at all.  Although Swift supporting "protocols" (traits, interfaces) makes the language more "composition-friendly", it also includes class inheritance.  This makes Swift friendlier to programmers used to traditional object-oriented programming techniques using inheritance, at the cost of complicated rules for things such as initializers and access control.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, with all that out of the way, let's focus on language comparisons drawn by following along in the &lt;a href="https://docs.swift.org/swift-book/"&gt;Swift Book&lt;/a&gt; and comparing to Rust.  In many places, I will abbreviate by showing Swift vs. Rust side-by-side with an "em (long) dash" (—) between.  Sorry if the formatting isn't too great; it works for me. &lt;code&gt;¯\_(ツ)_/¯&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://docs.swift.org/swift-book/LanguageGuide/TheBasics.html"&gt;The Basics&lt;/a&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Fundamental types
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Int&lt;/code&gt; — &lt;code&gt;isize&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;UInt&lt;/code&gt; — &lt;code&gt;usize&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Int8&lt;/code&gt;, &lt;code&gt;Int16&lt;/code&gt;, &lt;code&gt;Int32&lt;/code&gt;, &lt;code&gt;Int64&lt;/code&gt; — &lt;code&gt;i8&lt;/code&gt;, &lt;code&gt;i16&lt;/code&gt;, &lt;code&gt;i32&lt;/code&gt;, &lt;code&gt;i64&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;UInt8&lt;/code&gt;, &lt;code&gt;UInt16&lt;/code&gt;, &lt;code&gt;UInt32&lt;/code&gt;, &lt;code&gt;UInt64&lt;/code&gt; — &lt;code&gt;u8&lt;/code&gt;, &lt;code&gt;u16&lt;/code&gt;, &lt;code&gt;u32&lt;/code&gt;, &lt;code&gt;u64&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Uint32.min&lt;/code&gt;, &lt;code&gt;Uint32.max&lt;/code&gt; — &lt;code&gt;u32::MIN&lt;/code&gt;, &lt;code&gt;u32::MAX&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Double&lt;/code&gt;, &lt;code&gt;Float&lt;/code&gt; — &lt;code&gt;f64&lt;/code&gt;, &lt;code&gt;f32&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;Literals can be in hexidecimal: &lt;code&gt;0xFp-2&lt;/code&gt; == 3.75&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Bool&lt;/code&gt; — &lt;code&gt;bool&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Optionals
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;T?&lt;/code&gt; — &lt;code&gt;Option&amp;lt;T&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;nil&lt;/code&gt; — &lt;code&gt;None&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;value == nil&lt;/code&gt;, &lt;code&gt;value != nil&lt;/code&gt; — &lt;code&gt;value.is_none()&lt;/code&gt;, &lt;code&gt;value.is_some()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Optional binding:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;let possible_value: Int? = Int("123")&lt;/code&gt; — &lt;code&gt;let possible_value = "123".parse().ok()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;if let value = possible_value&lt;/code&gt; — &lt;code&gt;if let Some(value) = possible_value&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;You can have many optional bindings in a single conditional:

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;if let first = Int("4"), let second = Int("42"), first &amp;lt; second&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Unwrapping values (&lt;code&gt;!&lt;/code&gt; — &lt;code&gt;unwrap()&lt;/code&gt;):

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;let value: Int = Int("123")!&lt;/code&gt; — &lt;code&gt;let value = "123".parse().ok().unwrap()&lt;/code&gt; or just &lt;code&gt;let value = "123".parse().unwrap()&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;"Implicitly unwrapped optionals" are types that are unwrapped automatically if used in a non-optional context, but left optional otherwise:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;let x: Int! = Int("123")&lt;/code&gt; &amp;lt;-- adding &lt;code&gt;!&lt;/code&gt; to type makes it an implicitly unwrapped optional&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;let y: Int = x&lt;/code&gt; &amp;lt;-- implicitly unwrapped&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;if let z = x&lt;/code&gt; &amp;lt;-- left optional, tested in conditional&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Constants and Variables
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;"Constant" is an immutable value, with the same syntax:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;let name = 10&lt;/code&gt; — &lt;code&gt;let name = 10&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;"Variable" is a mutable value:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;var name = 10&lt;/code&gt; — &lt;code&gt;let mut name = 10&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;You can have multiple declarations on a single line:

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;var x = 0, y = 1, z = 2&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Type annotations are the same, except you can define multiple values that share the same type annotation:

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;var x, y, z: Int&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Constant and variable names can contain almost any character:

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;let π = 3.14159&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;let 你好 = "你好世界"&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;let 🐶🐮 = "dogcow"&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;No name shadowing allowed&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;print&lt;/code&gt; is a combination &lt;code&gt;println!&lt;/code&gt; and &lt;code&gt;print!&lt;/code&gt; where it behaves like &lt;code&gt;println!&lt;/code&gt; by default but you can set a custom &lt;code&gt;terminator&lt;/code&gt; argument to &lt;code&gt;""&lt;/code&gt; to be like &lt;code&gt;print!&lt;/code&gt; or something else.&lt;/li&gt;
&lt;li&gt;Interpolation is intrinsically supported for strings:

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;print("The answer is \(add(40, 2))")&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Note that the interpolation can be any expression, including literals and function calls.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Comments
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Comments are the same, including ability to nest &lt;code&gt;/* */&lt;/code&gt; style comments.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Semicolons
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Swift only requires a semicolon at the end of a statement if another statement follows it on the same line.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Type casts
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;(Double)name&lt;/code&gt; — &lt;code&gt;name as f64&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Some type casts return optionals:

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;let possible_number: Int? = Int("123")&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Type aliases
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;typealias AudioSample = UInt16&lt;/code&gt; — &lt;code&gt;type AudioSample = u16&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Tuples
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Tuples are the same except you can also name the parts of tuple literals:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;statuscode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"OK"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Status: &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;statuscode&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt; (&lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;description&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt;)"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Error Handling
&lt;/h3&gt;

&lt;p&gt;Here we have the first fundamental difference between Swift and Rust.  In Rust, errors are handled by representing them as values of the &lt;code&gt;Result::Error&lt;/code&gt; type variant and returning them up the call chain through &lt;code&gt;Result&lt;/code&gt; values.  In other words, successful return values and error return values essentially follow the same path out of function calls.  In Swift, errors are represented by values conforming to the &lt;code&gt;Error&lt;/code&gt; protocol (trait) that are "thrown" at one point and "caught" (or not) somewhere up the call chain.  They follow a fundamentally separate path from successful return values.&lt;/p&gt;

&lt;p&gt;A function that can throw an error must have the keyword &lt;code&gt;throws&lt;/code&gt; placed in its declaration (before the body):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;func can_throw() throws {&lt;/code&gt; vs &lt;code&gt;func does_not_throw() {&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When calling such a function, you must prefix the call with the &lt;code&gt;try&lt;/code&gt; keyword:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;try can_throws()&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To automatically catch an error at the call site and convert it to an optional, use &lt;code&gt;try?&lt;/code&gt; (equivalent to &lt;code&gt;Result::ok()&lt;/code&gt; on the return value of a function in Rust):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;let x: Int? = try? something()&lt;/code&gt; — &lt;code&gt;let x: Option&amp;lt;isize&amp;gt; = something().ok()&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To automatically catch an error at the call site and terminate with a runtime error, use &lt;code&gt;try!&lt;/code&gt; (equivalent to &lt;code&gt;Result::unwrap()&lt;/code&gt; on the return value of a function in Rust):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;let x: Int = try! something()&lt;/code&gt; — &lt;code&gt;let x: isize = something().unwrap()&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Throw errors with the &lt;code&gt;throw&lt;/code&gt; statement:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;throw some_error&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Unless caught at the call site with &lt;code&gt;try?&lt;/code&gt; or &lt;code&gt;try!&lt;/code&gt;, errors "unwind the stack" or cause code execution to return out of functions until explicitly "caught". You catch errors outside the call site using a "do-catch" statement:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="nf"&gt;something&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="n"&gt;some_error&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;handle_specified_error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;some_error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;handle_unspecified_error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that the special name &lt;code&gt;error&lt;/code&gt; is available in the unspecified error handling block.&lt;/p&gt;

&lt;h3&gt;
  
  
  Assertions and Preconditions
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;assert&lt;/code&gt; and &lt;code&gt;precondition&lt;/code&gt; are like the &lt;code&gt;assert!&lt;/code&gt; macro, except that &lt;code&gt;assert&lt;/code&gt; is only executed for a "Debug" build.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;preconditionFailure&lt;/code&gt; is essentially a &lt;code&gt;panic!&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://docs.swift.org/swift-book/LanguageGuide/BasicOperators.html"&gt;Basic Operators&lt;/a&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Swift has the ternary operator &lt;code&gt;a ? b : c&lt;/code&gt;, unlike Rust where you would instead use a conditional expression &lt;code&gt;if a { b } else { c }&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;String&lt;/code&gt; type implements the &lt;code&gt;+&lt;/code&gt; operator for concatenation; in Rust the &lt;code&gt;+&lt;/code&gt; operator (or &lt;code&gt;String::push_str&lt;/code&gt;) requires the right-hand side to be a slice.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Bool&lt;/code&gt; type does not support comparison, unlike &lt;code&gt;bool&lt;/code&gt; in Rust.&lt;/li&gt;
&lt;li&gt;"Nil-Coalescing Operator": &lt;code&gt;a ?? b&lt;/code&gt; — &lt;code&gt;a.unwrap_or_else(|| b)&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;Note that &lt;code&gt;b&lt;/code&gt; is not evaluated unless &lt;code&gt;a&lt;/code&gt; is &lt;code&gt;nil&lt;/code&gt;, which is why it maps to &lt;code&gt;Option::unwrap_or_else&lt;/code&gt; rather than &lt;code&gt;Option::unwrap_or&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Range operators

&lt;ul&gt;
&lt;li&gt;Closed range: &lt;code&gt;1...5&lt;/code&gt; — &lt;code&gt;1..=5&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Half-open range: &lt;code&gt;1..&amp;lt;5&lt;/code&gt; — &lt;code&gt;1..5&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;One-sided range: &lt;code&gt;1...&lt;/code&gt;, &lt;code&gt;...1&lt;/code&gt; — &lt;code&gt;1..&lt;/code&gt;, &lt;code&gt;..1&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;There isn't a Swift equivalent of the Rust &lt;code&gt;RangeFull&lt;/code&gt; (unbounded range &lt;code&gt;..&lt;/code&gt;)?&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://docs.swift.org/swift-book/LanguageGuide/StringsAndCharacters.html"&gt;Strings and Characters&lt;/a&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Swift strings are "composed of encoding-independent Unicode characters" and "built from Unicode scalar values", whereas in Rust &lt;code&gt;String&lt;/code&gt; and &lt;code&gt;str&lt;/code&gt; are UTF-8 encoded.&lt;/li&gt;
&lt;li&gt;A Swift &lt;code&gt;Character&lt;/code&gt; is a single extended grapheme cluster (sequence of one or more Unicode scalars), whereas a Rust &lt;code&gt;char&lt;/code&gt; is a single Unicode scalar.  For example, in Rust, &lt;code&gt;"café".chars().count()&lt;/code&gt; is 5 and &lt;code&gt;"café".len()&lt;/code&gt; is 6, whereas in Swift, &lt;code&gt;"café".count&lt;/code&gt; is 4.

&lt;ul&gt;
&lt;li&gt;The letter &lt;code&gt;é&lt;/code&gt; is a single grapheme cluster composed of the Unicode scalar "LATIN SMALL LETTER E" (U+0065), which is one byte when UTF-8 encoded, followed by the Unicode scalar "COMBINING ACUTE ACCENT" (U+0301), which is two bytes when UTF-8 encoded.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;In Swift you can modify characters in strings by concatenating Unicode scalars together.  For example, &lt;code&gt;"e" + "\u{301}" == "é"&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Swift &lt;code&gt;String&lt;/code&gt; has an associated type &lt;code&gt;String.Index&lt;/code&gt; representing the position of a &lt;code&gt;Character&lt;/code&gt; in the &lt;code&gt;String&lt;/code&gt;.

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;String&lt;/code&gt; methods &lt;code&gt;index(before:)&lt;/code&gt;, &lt;code&gt;index(after:)&lt;/code&gt;, and &lt;code&gt;index(_:offsetBy:)&lt;/code&gt; are used with &lt;code&gt;String&lt;/code&gt; properties &lt;code&gt;startIndex&lt;/code&gt; and &lt;code&gt;endIndex&lt;/code&gt; to construct &lt;code&gt;String.Index&lt;/code&gt; values.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;indices&lt;/code&gt; property of &lt;code&gt;String&lt;/code&gt; in Swift iterates over characters just like &lt;code&gt;str::chars&lt;/code&gt; in Rust.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Indexing or slicing a string returns a &lt;code&gt;Substring&lt;/code&gt; which is similar to &lt;code&gt;str&lt;/code&gt; (holds a reference to the &lt;code&gt;String&lt;/code&gt; it was sliced from).

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;let beginning = line[..&amp;lt;index]&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Converting &lt;code&gt;Substring&lt;/code&gt; to &lt;code&gt;String&lt;/code&gt; is a type cast&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;You can encode &lt;code&gt;String&lt;/code&gt; to UTF-8 or UTF-16 with the &lt;code&gt;utf8&lt;/code&gt; or &lt;code&gt;utf16&lt;/code&gt; properties which are &lt;code&gt;String.UTF8View&lt;/code&gt; or &lt;code&gt;String.UTF16View&lt;/code&gt; and are iterable as a sequence of &lt;code&gt;UInt8&lt;/code&gt; or &lt;code&gt;UInt16&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;You can get the Unicode scalar values of a &lt;code&gt;String&lt;/code&gt; with the &lt;code&gt;unicodeScalars&lt;/code&gt; property which is &lt;code&gt;UnicodeScalarView&lt;/code&gt; and is iterable as a sequence of &lt;code&gt;UnicodeScalar&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Multi-line strings drop indented space, which Rust doesn't do inherently, but you can get the equivalent behavior if you use the
&lt;a href="https://docs.rs/indoc"&gt;indoc&lt;/a&gt; crate.&lt;/li&gt;
&lt;li&gt;Swift strings are value types, which are copied when passed to functions. There is no type distinction between mutable and immutable strings, like there is in Rust (with &lt;code&gt;String&lt;/code&gt; being mutable and &lt;code&gt;str&lt;/code&gt; (slice) being immutable).

&lt;ul&gt;
&lt;li&gt;Note that the book mentions, "Swift’s compiler optimizes string usage so that actual copying takes place only when absolutely necessary", which implies the implementation is perhaps closer to &lt;code&gt;Cow&amp;lt;'a, str&amp;gt;&lt;/code&gt;?&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;"Extended String Delimiters" are similar to "raw string literals" in that special characters can be placed in the string without invoking their effect. However, in Swift you can "manually invoke an effect" by adding a matching number of &lt;code&gt;#&lt;/code&gt; pound signs following the &lt;code&gt;\&lt;/code&gt; backslash escape character:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;#"Line 1\nLine 2"#&lt;/code&gt; — &lt;code&gt;r#"Line 1\nLine 2"#&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;##"Line 1\nLine 2"##&lt;/code&gt; — &lt;code&gt;r##"Line 1\nLine 2"##&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;#"Line 1\#nLine 2"#&lt;/code&gt; breaks the line and is equivalent to &lt;code&gt;"Line 1\nLine 2"&lt;/code&gt; in Swift or Rust; no way to do this with a Rust raw string literal?&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://docs.swift.org/swift-book/LanguageGuide/CollectionTypes.html"&gt;Collection Types&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;There are three primary collection types in Swift:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Array&amp;lt;T&amp;gt;&lt;/code&gt; or &lt;code&gt;[T]&lt;/code&gt; (shorthand) — &lt;code&gt;Vec&amp;lt;T&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Set&amp;lt;T&amp;gt;&lt;/code&gt; — &lt;code&gt;HashSet&amp;lt;T&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Dictionary&amp;lt;Key, Value&amp;gt;&lt;/code&gt; or &lt;code&gt;[Key: Value]&lt;/code&gt; (shorthand) — &lt;code&gt;HashMap&amp;lt;K, V&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The only ordered collection type is the &lt;code&gt;Array&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To order the keys of a &lt;code&gt;Set&lt;/code&gt;, use the &lt;code&gt;sorted()&lt;/code&gt; method, which returns an &lt;code&gt;Array&lt;/code&gt; of the keys sorted in order.&lt;/li&gt;
&lt;li&gt;To order the contents of a &lt;code&gt;Dictionary&lt;/code&gt;, use the &lt;code&gt;sorted()&lt;/code&gt; method of the &lt;code&gt;keys&lt;/code&gt; or &lt;code&gt;values&lt;/code&gt; properties, which return an &lt;code&gt;Array&lt;/code&gt; of the keys or values sorted in order.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The keys of &lt;code&gt;Set&lt;/code&gt; and &lt;code&gt;Dictionary&lt;/code&gt; need to be hashable, just as in Rust.  The protocol &lt;a href="https://developer.apple.com/documentation/swift/hashable"&gt;&lt;code&gt;Hashable&lt;/code&gt;&lt;/a&gt; in Swift is like the trait &lt;a href="https://doc.rust-lang.org/std/hash/trait.Hash.html"&gt;&lt;code&gt;Hash&lt;/code&gt;&lt;/a&gt; in Rust.&lt;/p&gt;

&lt;h3&gt;
  
  
  Using Arrays
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;[Int32]()&lt;/code&gt; — &lt;code&gt;Vec::&amp;lt;i32&amp;gt;::new()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Array(repeating: 0.0, count: 3)&lt;/code&gt; — &lt;code&gt;vec![0.0; 3]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Array&lt;/code&gt; supports the &lt;code&gt;+&lt;/code&gt; and &lt;code&gt;+=&lt;/code&gt; operators to concatenate and append, whereas in Rust you would have to start with the left-hand side and then either &lt;code&gt;append&lt;/code&gt; or &lt;code&gt;extend&lt;/code&gt; it.&lt;/li&gt;
&lt;li&gt;You can use subscript syntax to replace a value at a given index, just as in Rust:

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;values[4] = "apples"&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;You can also use subscript syntax to splice:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;values[4...6] = ["apples", "oranges"]&lt;/code&gt; — &lt;code&gt;values.splice(4..=6, ["apples", "oranges"].iter().cloned());&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;removeLast()&lt;/code&gt; — &lt;code&gt;Vec::pop&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Using Sets
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Set&amp;lt;Int32&amp;gt;()&lt;/code&gt; — &lt;code&gt;HashSet::&amp;lt;i32&amp;gt;::new()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;let values: Set&amp;lt;Int&amp;gt; = [1, 2, 3]&lt;/code&gt; — &lt;code&gt;let values = hashset!{1, 2, 3}&lt;/code&gt; (using the &lt;a href="https://docs.rs/maplit/"&gt;maplit&lt;/a&gt; crate)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;count&lt;/code&gt; — &lt;code&gt;HashSet::len&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;substracting(_:)&lt;/code&gt; — &lt;code&gt;HashSet::difference&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;isStrictSubset(of:)&lt;/code&gt; and &lt;code&gt;isStrictSuperset(of:)&lt;/code&gt; are like &lt;code&gt;isSubset(of:)&lt;/code&gt; and &lt;code&gt;isSuperset(of:)&lt;/code&gt; except they return &lt;code&gt;false&lt;/code&gt; if the sets are equal.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Using Dictionaries
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;[Int32: String]()&lt;/code&gt; — &lt;code&gt;HashMap::&amp;lt;i32, String&amp;gt;::new()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[:]&lt;/code&gt; — &lt;code&gt;HashMap::new()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;let dict = [1: "apples", 2: "oranges", 3: "bananas"]&lt;/code&gt; — &lt;code&gt;let dict = hashmap!{1 =&amp;gt; "apples", 2 =&amp;gt; "oranges", 3 =&amp;gt; "bananas"}&lt;/code&gt; (using the &lt;a href="https://docs.rs/maplit/"&gt;maplit&lt;/a&gt; crate)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;count&lt;/code&gt; — &lt;code&gt;HashMap::len&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;dict.updateValue("answer", forKey: 42)&lt;/code&gt; (or &lt;code&gt;dict[42] = "answer"&lt;/code&gt; if the optional old value is not needed) — &lt;code&gt;dict.insert(42, "answer")&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;dict[42]&lt;/code&gt; — &lt;code&gt;dict.get(42)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;dict[42]!&lt;/code&gt; — &lt;code&gt;dict[42]&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://docs.swift.org/swift-book/LanguageGuide/ControlFlow.html"&gt;Control Flow&lt;/a&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Control flow uses statement semantics, so they can't be used in expression contexts.

&lt;ul&gt;
&lt;li&gt;For example, this is valid in Rust but not Swift: &lt;code&gt;let x = if y { 1 } else { 2 }&lt;/code&gt; (one could use the ternary operator &lt;code&gt;y ? 1 : 2&lt;/code&gt; instead)&lt;/li&gt;
&lt;li&gt;Another consequence of this is that &lt;code&gt;break&lt;/code&gt; statements cannot accept an argument.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Swift has an additional "repeat-while" syntax similar to the "do-while" syntax in C, but lacks the &lt;code&gt;loop&lt;/code&gt; infinite loop and &lt;code&gt;while let&lt;/code&gt; syntax.&lt;/li&gt;
&lt;li&gt;Label names for loops (where a &lt;code&gt;continue&lt;/code&gt; or &lt;code&gt;break&lt;/code&gt; in a nested loop can jump out to a labelled outer loop) must begin with a quote &lt;code&gt;'&lt;/code&gt; character in Rust, but not in Swift.&lt;/li&gt;
&lt;li&gt;A Swift &lt;code&gt;switch&lt;/code&gt; is similar to a Rust &lt;code&gt;match&lt;/code&gt; except:

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;case&lt;/code&gt; keyword is present in front of each arm, similar to C.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;default&lt;/code&gt; keyword, without &lt;code&gt;case&lt;/code&gt;, takes the place of &lt;code&gt;_&lt;/code&gt; to match any cases not otherwise covered.&lt;/li&gt;
&lt;li&gt;The body of each case must have at least one statement.&lt;/li&gt;
&lt;li&gt;Alternative patterns are separated by comma &lt;code&gt;,&lt;/code&gt; rather than pipe &lt;code&gt;|&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Value binding in patterns requires placing the &lt;code&gt;let&lt;/code&gt; keyword before the name to bind:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;case (let x, 0):&lt;/code&gt; — &lt;code&gt;(x, 0) =&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Swift uses the &lt;code&gt;where&lt;/code&gt; keyword in place of &lt;code&gt;if&lt;/code&gt; for "additional conditions" (called "match guards" in Rust).

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;case let (x, y) where x == y:&lt;/code&gt; — &lt;code&gt;(x, y) if x == y =&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Swift has an additional control transfer statement &lt;code&gt;fallthrough&lt;/code&gt; you can use as the last statement of a &lt;code&gt;case&lt;/code&gt; in a &lt;code&gt;switch&lt;/code&gt; in order to mimic the "fall through" behavior of a &lt;code&gt;switch&lt;/code&gt; in C where the lack of a &lt;code&gt;break&lt;/code&gt; causes execution to flow from the end of one case to the beginning of the next case.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Swift has an additional "guard" syntax intended for use in "early exit" scenarios.  It looks simiar to an &lt;code&gt;if&lt;/code&gt; statement, except &lt;code&gt;if&lt;/code&gt; is replaced by &lt;code&gt;guard&lt;/code&gt;, the &lt;code&gt;else&lt;/code&gt; keyword is placed before the body, and any value bound to the condition (with &lt;code&gt;guard let&lt;/code&gt;) is scoped to the block containing the guard. The body statements are only executed if the condition is &lt;code&gt;false&lt;/code&gt;, and typically contain an "early exit" return.&lt;/li&gt;
&lt;li&gt;Conditional compilation in Swift is done using a special conditional you can use in &lt;code&gt;if&lt;/code&gt; and &lt;code&gt;guard&lt;/code&gt; statements, as opposed to the &lt;code&gt;cfg&lt;/code&gt; attribute in Rust.

&lt;ul&gt;
&lt;li&gt;The only special conditional mentioned in the book for use in conditional compilation is the &lt;code&gt;#available&lt;/code&gt; condition used to conditionally compile based on the target platform.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://docs.swift.org/swift-book/LanguageGuide/Functions.html"&gt;Functions&lt;/a&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;func&lt;/code&gt; — &lt;code&gt;fn&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;If the entire body of the function is a single expression, the function implicitly returns that expression.  Rust supports this, but also allows there to be statements before the expression, which Swift doesn't support.&lt;/li&gt;
&lt;li&gt;Arguments are "labelled", and such argument labels are considered part of the function name.  This gives them an "Objective-C" flavor designed for compatibility with that language.

&lt;ul&gt;
&lt;li&gt;For example: &lt;code&gt;func speak(toPerson person: String, what speech: String)&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;The name for this function overall is considered to be &lt;code&gt;speak(toPerson:what:)&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;If unspecified, the label is the same as the name:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;func speak(person: String, what speech: String)&lt;/code&gt; is the same as &lt;code&gt;func speak(person person: String, what speech: String)&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;A label may be omitted by replacing it with an underscore (&lt;code&gt;_&lt;/code&gt;) in the function declaration.

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;func speakTo(_ person: String, what speech: String)&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;In this case the function name is considered to be &lt;code&gt;speakTo(_:what:)&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Labels not omitted must be used when calling the function.

&lt;ul&gt;
&lt;li&gt;For example: &lt;code&gt;speakTo("Frank", what: "Hello!")&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Although names must be unique, labels need not be (but usually are for readability).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Arguments can have default values specified in the function declaration by adding after the type an equals sign (&lt;code&gt;=&lt;/code&gt;) followed by a value.  If an argument has a default value, it can be omitted when calling the function.&lt;/li&gt;
&lt;li&gt;Functions support variadic parameters.  If the type of a parameter is followed by &lt;code&gt;...&lt;/code&gt;, the actual type of the parameter will be an &lt;code&gt;Array&lt;/code&gt; of the specified type, while callers provide the values for the array without using array syntax.

&lt;ul&gt;
&lt;li&gt;Note that a function can have at most one variadic parameter, but it doesn't necessary need to be the last parameter?
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;average&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Double&lt;/span&gt;&lt;span class="o"&gt;...&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Double&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cm"&gt;/* body omitted for brevity */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nf"&gt;average&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;average&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;1.2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;3.5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Parameters are constants (immutable) by default.&lt;/li&gt;
&lt;li&gt;Parameters are passed by value (copied, as in the &lt;code&gt;Copy&lt;/code&gt; trait in Rust) if they are fundamental types, strings, or structures.  Otherwise, they are passed by reference.&lt;/li&gt;
&lt;li&gt;Placing the &lt;code&gt;inout&lt;/code&gt; keyword before a parameter type in the function declaration gives the parameter a "copy-in copy-out" semantic (which may be optimized into "pass by mutable reference", but you shouldn't count on this).

&lt;ul&gt;
&lt;li&gt;When the function is called, the parameter is copied.&lt;/li&gt;
&lt;li&gt;During the function call, the parameter is mutable and should be considered separate from the original value (although it may be optimized to be the same address in memory).&lt;/li&gt;
&lt;li&gt;When the function returns, the parameter is copied back to caller, replacing the original value.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;For the purpose of identifying the type of a function with no return value, &lt;code&gt;Void&lt;/code&gt; takes the place of &lt;code&gt;()&lt;/code&gt; as the type returned.&lt;/li&gt;
&lt;li&gt;Functions passed as parameters are declared without any keyword like &lt;code&gt;func&lt;/code&gt;:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;func useFunction(_ function: (Int) -&amp;gt; Int)&lt;/code&gt; — &lt;code&gt;fn use_function(function: Fn(isize) -&amp;gt; isize)&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Unlike in Rust, there is no distinction in Swift between "by-value" (&lt;code&gt;FnOnce&lt;/code&gt;), "mutable" (&lt;code&gt;FnMut&lt;/code&gt;), "immutable" (&lt;code&gt;Fn&lt;/code&gt;), or "function pointer" (&lt;code&gt;fn&lt;/code&gt;) closure/function types.&lt;/li&gt;
&lt;li&gt;A function declared inside another function is technically a closure in Swift (has access to all values in the scope of the outer function), whereas in Rust such a function is not a closure (although you could make the inner function a closure in Rust to do the same thing).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://docs.swift.org/swift-book/LanguageGuide/Closures.html"&gt;Closures&lt;/a&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;{(x: Int, y: Int) -&amp;gt; Int in x * y}&lt;/code&gt; — &lt;code&gt;|x: isize, y: isize| x * y&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;{x, y in x * y}&lt;/code&gt; — &lt;code&gt;|x, y| x * y&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Argument names can be omitted entirely if types can be implied; in this case special names formed by &lt;code&gt;$&lt;/code&gt; followed by argument index represent each argument

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;{$0 * $1}&lt;/code&gt; — &lt;code&gt;|x, y| x * y&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Operator methods are probably the shortest possible syntax for a closure.  For example, &lt;code&gt;names.sorted(by: &amp;gt;)&lt;/code&gt; sorts &lt;code&gt;names&lt;/code&gt; using the &lt;code&gt;&amp;gt;&lt;/code&gt; operator of the type of the items in &lt;code&gt;names&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Swift has a "trailing closure" syntax where you can move a closure parameter to a block immediately after the parentheses of the function call.  For example, &lt;code&gt;names.sorted(by: { $0 &amp;gt; $1 })&lt;/code&gt; can be written as &lt;code&gt;names.sorted() { $0 &amp;gt; $1 }&lt;/code&gt; instead.

&lt;ul&gt;
&lt;li&gt;The argument label for the first trailing closure is omitted.  If there are additional trailing closures, they follow the first trailing closure body and are proceeded by their argument labels.&lt;/li&gt;
&lt;li&gt;If there are no arguments besides the trailing closure(s), the parentheses of the function call itself can be omitted as well.  For example: &lt;code&gt;names.sorted{ $0 &amp;gt; $1 }&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Closures capture constants and variables from the surrounding context by reference.  As an optimization, the compiler may capture them by value instead, if they are not mutated by the closure or any code executed after the closure is created.&lt;/li&gt;
&lt;li&gt;Closures are reference types (may be obvious, but the book has a special section to emphasize this point).&lt;/li&gt;
&lt;li&gt;Closures can have a "capture list" where you explicitly control what values from the surrounding context are captured and how they are captured.

&lt;ul&gt;
&lt;li&gt;If present, the capture list comes first inside the opening curly brace (&lt;code&gt;{&lt;/code&gt;) of the closure.&lt;/li&gt;
&lt;li&gt;Inside square brackets, place a comma-separated list of items, where each item is a value to capture, optionally with the &lt;code&gt;weak&lt;/code&gt; or &lt;code&gt;unowned&lt;/code&gt; keyword in front to specify whether the reference should be weak or not counted at all (respectively).  Each value may also be bound to a name local to the closure.  For example:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;[self]&lt;/code&gt; — capture value &lt;code&gt;self&lt;/code&gt; by strong reference&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[weak self]&lt;/code&gt; — capture value &lt;code&gt;self&lt;/code&gt; by weak reference&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[weak delegate = self.delegate]&lt;/code&gt; — capture value &lt;code&gt;self.delegate&lt;/code&gt; by weak reference named &lt;code&gt;delegate&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;A closure which "escapes" a function (called after the function returns) must be marked with &lt;code&gt;@escaping&lt;/code&gt; before the closure's type in the function parameter list.&lt;/li&gt;
&lt;li&gt;Marking a closure parameter with &lt;code&gt;@autoclosure&lt;/code&gt; before the closure's type in a function parameter list allows you to call the function and pass an expression that is automatically wrapped by a closure.  In other words, the expression passed to the function is "lazily evaluated", or not evaluated until and unless the closure is called by the function.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://docs.swift.org/swift-book/LanguageGuide/Enumerations.html"&gt;Enumerations&lt;/a&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;When listing variants of enums, each line must begin with the &lt;code&gt;case&lt;/code&gt; keyword.

&lt;ul&gt;
&lt;li&gt;Multiple variants may be declared per line, if they are comma-separated.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;The syntax for specifying a variant is &lt;code&gt;TypeName.variant&lt;/code&gt; or just &lt;code&gt;.variant&lt;/code&gt; if &lt;code&gt;TypeName&lt;/code&gt; can be inferred.&lt;/li&gt;
&lt;li&gt;By adding &lt;code&gt;: CaseIterable&lt;/code&gt; after the name of an enum in its declaration (in other words, specifying that the enum conforms to the &lt;code&gt;CaseIterable&lt;/code&gt; protocol), Swift will add an &lt;code&gt;allCases&lt;/code&gt; property which is a collection of all the variants.&lt;/li&gt;
&lt;li&gt;Enum values can be printed without implementing any protocol.&lt;/li&gt;
&lt;li&gt;In a &lt;code&gt;switch&lt;/code&gt; on an enum, the associated values (if any) are bound to names by placing &lt;code&gt;let&lt;/code&gt; followed by a name in the case pattern, or by placing a single &lt;code&gt;let&lt;/code&gt; immediately following the &lt;code&gt;case&lt;/code&gt; keyword.&lt;/li&gt;
&lt;li&gt;As an alternative to associated types, enums can handle "raw values" by adding a colon followed by raw value type after the name of the enum.  Each variant gets a corresponding raw value, which can be either assigned by default or assigned by providing a literal value after the variant name with an equals sign (&lt;code&gt;=&lt;/code&gt;) between the name and value.  Raw value types can be characters, strings, integers, or floating-point numbers.  Variants not assigned an explicit raw value get one assigned according to their position and type:

&lt;ul&gt;
&lt;li&gt;Integers default to &lt;code&gt;0&lt;/code&gt; for the first variant or the value of the previous variant plus one.&lt;/li&gt;
&lt;li&gt;Strings default to the name of the variant itself.&lt;/li&gt;
&lt;li&gt;Raw value type enums automatically get an initializer method that takes a raw value of the proper type and returns an optional enum value (the variant that corresponds to the given raw value, or &lt;code&gt;nil&lt;/code&gt; if no there is no corresponding variant for the raw value).&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;An enum variant can have an associated value that has the same enum type, as long as the variant is marked with the &lt;code&gt;indirect&lt;/code&gt; keyword before the &lt;code&gt;case&lt;/code&gt; of the variant.  This causes Swift to add indirection to the variant, just as you would have to do manually using &lt;code&gt;Box&amp;lt;T&amp;gt;&lt;/code&gt; in Rust to achieve the same thing.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://docs.swift.org/swift-book/LanguageGuide/ClassesAndStructures.html"&gt;Structures and Classes&lt;/a&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Both structures and classes in Swift are like structures in Rust.&lt;/li&gt;
&lt;li&gt;Classes have additional capabilities that structures don't have:

&lt;ul&gt;
&lt;li&gt;Inheritance (not directly supported in Rust at all)&lt;/li&gt;
&lt;li&gt;Type casting&lt;/li&gt;
&lt;li&gt;Deinitializers (analogous to the &lt;code&gt;Drop&lt;/code&gt; trait in Rust)&lt;/li&gt;
&lt;li&gt;Reference counting (analogous to the &lt;code&gt;Rc&lt;/code&gt; and &lt;code&gt;Arc&lt;/code&gt; types in Rust)

&lt;ul&gt;
&lt;li&gt;Structures (and enumerations) are "value types", meaning they are copied when assigned or passed to functions.&lt;/li&gt;
&lt;li&gt;Classes are "reference types" meaning they behave as if implicitly wrapped in an &lt;code&gt;Rc&lt;/code&gt;/&lt;code&gt;Arc&lt;/code&gt;, where a reference is added when assigned or passed to functions, and the value is only deallocated when the reference count goes to zero.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Use the &lt;code&gt;class&lt;/code&gt; keyword in place of &lt;code&gt;struct&lt;/code&gt; to make a class, similar to C++.&lt;/li&gt;
&lt;li&gt;Fields are defined (and may be initialized as well) in the type definition using the same syntax as variable declaration and assignment, including the ability to let the compiler infer types (unlike in Rust where each field's type must be explicitly defined, and initialization happens in a method or explicit structure instantiation).&lt;/li&gt;
&lt;li&gt;To distinquish equality from identity, Swift defines the operator &lt;code&gt;===&lt;/code&gt; (and inverse &lt;code&gt;!==&lt;/code&gt;) for references, where &lt;code&gt;===&lt;/code&gt; is like &lt;code&gt;Arc::ptr_eq&lt;/code&gt;, returning &lt;code&gt;true&lt;/code&gt; if the operands refer to the same value.&lt;/li&gt;
&lt;li&gt;Swift references are automatically dereferenced when used; there is no syntax for "dereferencing" or for creating a new reference (other than assigning a reference or passing it to a function).  In other words, references are not first class types in Swift?&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://docs.swift.org/swift-book/LanguageGuide/Properties.html"&gt;Properties&lt;/a&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Properties are either "stored" (as in Rust) or "computed".

&lt;ul&gt;
&lt;li&gt;A stored property marked with the &lt;code&gt;lazy&lt;/code&gt; keyword before its declaration is not calculated until the first time it's used.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;A "computed" property can be defined for classes, structures, and enum types.  They are declarated similarly to properties with accessors in C# classes:

&lt;ul&gt;
&lt;li&gt;After the type name of the property is a block containing two inner blocks, one with &lt;code&gt;get&lt;/code&gt; before it and the other with &lt;code&gt;set&lt;/code&gt; before it.  These inner blocks are like functions called when the property is read or written, respectively.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;get&lt;/code&gt; block is expected to compute the value of the property and return it using a &lt;code&gt;return&lt;/code&gt; statement.

&lt;ul&gt;
&lt;li&gt;A shorthand syntax is supported where the inner block of the &lt;code&gt;get&lt;/code&gt; is a single expression.  In this case, that expression is evaluated to compute the value of the property, without a need for an explicit &lt;code&gt;return&lt;/code&gt; statement.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;set&lt;/code&gt; block gets an value argument which is the expression assigned to the property.  There are two ways of defining this value:

&lt;ul&gt;
&lt;li&gt;By placing a name in parentheses between &lt;code&gt;set&lt;/code&gt; and its code block, the value is given that name.&lt;/li&gt;
&lt;li&gt;Otherwise, an implicit value named &lt;code&gt;newValue&lt;/code&gt; is defined within the code block.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;A read-only computed property is declared by dropping the &lt;code&gt;get&lt;/code&gt; keyword, keeping the block of statements that would have come after &lt;code&gt;get&lt;/code&gt;, and dropping the &lt;code&gt;set&lt;/code&gt; keyword and its code block entirely.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Swift supports "property observers" which are like functions called automatically whenever a property's value is set.  They may be added for stored properties as well as inherited computed properties (for non-inherited computed properties, you put the observer code directly in the property's "setter").

&lt;ul&gt;
&lt;li&gt;Property observers are added in a syntax similar to computed properties, with a block after the type and default value (if any) of the property, and inner blocks proceeded by the &lt;code&gt;willSet&lt;/code&gt; and &lt;code&gt;didSet&lt;/code&gt; keywords.

&lt;ul&gt;
&lt;li&gt;Both &lt;code&gt;willSet&lt;/code&gt; and &lt;code&gt;didSet&lt;/code&gt; and their code blocks are optional.  You can have one or both.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;willSet&lt;/code&gt; block has the same syntax as the &lt;code&gt;set&lt;/code&gt; block (besides the keyword being different) and is called just before the property is set.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;didSet&lt;/code&gt; block has the same syntax as the &lt;code&gt;set&lt;/code&gt; block except for the keyword and the default name of the argument provided to it being &lt;code&gt;oldValue&lt;/code&gt; rather than &lt;code&gt;newValue&lt;/code&gt;.  The &lt;code&gt;didSet&lt;/code&gt; block is called just after the property is set.  The argument provided is the value the property had before it was set.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Swift supports "property wrappers" which are special structures which represent properties and provide reusable code for the getters and setters of properties.

&lt;ul&gt;
&lt;li&gt;They are declared like normal structures but with &lt;code&gt;@propertyWrapper&lt;/code&gt; in front.&lt;/li&gt;
&lt;li&gt;They must expose a computed property named &lt;code&gt;wrappedValue&lt;/code&gt; which provides the getter and setter for the wrapped property.&lt;/li&gt;
&lt;li&gt;Property wrappers are applied to a computed property by prefixing the computed property with the name of the property wrapper with &lt;code&gt;@&lt;/code&gt; in front (in other words, the name of the property wrapper turned into an attribute).&lt;/li&gt;
&lt;li&gt;Initializers provided with property wrappers can be used to set the initial value of wrapped properties.

&lt;ul&gt;
&lt;li&gt;A wrapped property with no default value provided uses the &lt;code&gt;init()&lt;/code&gt; initializer to initialize the property.&lt;/li&gt;
&lt;li&gt;A wrapped property with a single value assigned to it uses the &lt;code&gt;init(wrappedValue:)&lt;/code&gt; initializer to initialize the property.&lt;/li&gt;
&lt;li&gt;Other initializers may be used by expanding the property wrapper attribute applied to the computed property to look like a function call, with labels and values provided that need to match one of the &lt;code&gt;init&lt;/code&gt; initializers of the property wrapper.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;They support "projected values" which are exposed as if the type containing the computed property had a second read-only computed property with the same name but prefixed by a dollar sign (&lt;code&gt;$&lt;/code&gt;).  To add a projected value, the property wrapper declares its own property named &lt;code&gt;projectedValue&lt;/code&gt; which provides the projected value.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Computed properties and property observers can also be applied to global and local variables, which act like stored properties of structures.

&lt;ul&gt;
&lt;li&gt;Global variables act as if declared with the &lt;code&gt;lazy&lt;/code&gt; keyword, in that they are not initialized until used for the first time.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Swift supports "type properties" which are properties applying to types themselves (rather than values of a type).  These have the &lt;code&gt;static&lt;/code&gt; keyword added before the property declaration.

&lt;ul&gt;
&lt;li&gt;Type properties are accessed through the type name itself, as if the type was a structure.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://docs.swift.org/swift-book/LanguageGuide/Methods.html"&gt;Methods&lt;/a&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Classes, structures, and enums can have methods.&lt;/li&gt;
&lt;li&gt;Instance methods are like functions declared in an &lt;code&gt;impl&lt;/code&gt; block for a Rust type, except in Swift they are placed inside the type declaration itself, similar to how methods are declared in C++.&lt;/li&gt;
&lt;li&gt;Methods have access to the properties of their instance as if the names of the properties were within the scope of the method (similar to C++).&lt;/li&gt;
&lt;li&gt;A special implicit property &lt;code&gt;self&lt;/code&gt; is available to methods, bound to the instance itself.  This is similar to the &lt;code&gt;this&lt;/code&gt; value in C++ methods, and similar to &lt;code&gt;self&lt;/code&gt; in Rust &lt;code&gt;impl&lt;/code&gt; functions, except that it's implicit (as if declared as &lt;code&gt;&amp;amp;mut self&lt;/code&gt; for classes or &lt;code&gt;&amp;amp;self&lt;/code&gt; for structures and enums).&lt;/li&gt;
&lt;li&gt;To make &lt;code&gt;self&lt;/code&gt; mutable for structures and enums, add the &lt;code&gt;mutating&lt;/code&gt; keyword in front of the &lt;code&gt;func&lt;/code&gt; keyword in the method declaration.

&lt;ul&gt;
&lt;li&gt;Note that you cannot call a "mutating" method on a constant structure or enum.&lt;/li&gt;
&lt;li&gt;You can assign to &lt;code&gt;self&lt;/code&gt; in a "mutating" method to replace the value with a new one.  This can be useful, for example, in enums for changing the variant.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Type methods are like instance methods except the methods are called on the type itself, &lt;code&gt;self&lt;/code&gt; refers to the type, not any specific value of the type, and any unqualified method or property names used in the method refer to the other type-level methods and properties of the type, rather than of any specific value of the type.

&lt;ul&gt;
&lt;li&gt;To declare a type method, add the keyword &lt;code&gt;static&lt;/code&gt; in front of the method declaration.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Class methods are like type methods, except they can be overridden in subclasses (and therefore may only be declared in classes).

&lt;ul&gt;
&lt;li&gt;To declare a class method, add the keyword &lt;code&gt;class&lt;/code&gt; in front of the method declaration.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Unlike Rust, in Swift you can have "overloaded" methods, or multiple methods with the same name but different types for parameters and/or return value.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://docs.swift.org/swift-book/LanguageGuide/Subscripts.html"&gt;Subscripts&lt;/a&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Swift "subscripts" are similar to the &lt;code&gt;Index&lt;/code&gt; and &lt;code&gt;IndexMut&lt;/code&gt; traits in Rust, allowing types to define special methods to be used if instances are indexed.&lt;/li&gt;
&lt;li&gt;The syntax of subscripts are a mix of function and computed property syntax.

&lt;ul&gt;
&lt;li&gt;Their declarations begin like functions except there is no &lt;code&gt;func&lt;/code&gt; keyword, and the name is replaced by the &lt;code&gt;subscript&lt;/code&gt; keyword.&lt;/li&gt;
&lt;li&gt;They have &lt;code&gt;get&lt;/code&gt; and &lt;code&gt;set&lt;/code&gt; blocks in the body, similar to computed properties.

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;get&lt;/code&gt; block corresponds to &lt;code&gt;Index::index&lt;/code&gt; in Rust.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;set&lt;/code&gt; block corresponds to &lt;code&gt;IndexMut::index_mut&lt;/code&gt; in Rust, except rather than returning a reference, it takes a value to be assigned, like a computed property setter.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Subscripts can have multiple parameters, which are separated by commas both in the declaration and usage of the subscripts.&lt;/li&gt;
&lt;li&gt;A "type subscript" or "class subscript" is similar to a "type method" or "class method", respectively.  They're declared like a normal subscript, but with the &lt;code&gt;static&lt;/code&gt; or &lt;code&gt;class&lt;/code&gt; keyword in front of the declaration.  They enable indexing the overall type itself.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://docs.swift.org/swift-book/LanguageGuide/Inheritance.html"&gt;Inheritance&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;As inheritance is entirely foreign to Rust, this section will compare inheritance in Swift to inheritance in C++.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Properties can be overridden to provide custom getters, setters, or observers for any inherited property.  An overridden property has the &lt;code&gt;override&lt;/code&gt; keyword added similarly to an overriden method.

&lt;ul&gt;
&lt;li&gt;A read-only property may be overridden to be read-write.&lt;/li&gt;
&lt;li&gt;A read-write property &lt;em&gt;may not&lt;/em&gt; be overridden to be read-only.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Properties and methods can be prevented from being overridden by adding the &lt;code&gt;final&lt;/code&gt; keyword before their declarations in the superclass.&lt;/li&gt;
&lt;li&gt;An entire class can be prevented from being subclassed by adding the &lt;code&gt;final&lt;/code&gt; keyword before its declaration.&lt;/li&gt;
&lt;li&gt;There are no "pure virtual" classes or methods in Swift.  Use "protocols" instead.&lt;/li&gt;
&lt;li&gt;"Multiple inheritance" (a subclass having two or more superclasses) is not supported.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://docs.swift.org/swift-book/LanguageGuide/Initialization.html"&gt;Initialization&lt;/a&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;An "initializer" is similar to a C++ constructor; declared like a method, named &lt;code&gt;init&lt;/code&gt;, but without the &lt;code&gt;func&lt;/code&gt; keyword, and called when the type is used like a function, in order to ensure all properties of a new instance are initialized.  This is roughly analogous to the &lt;code&gt;new&lt;/code&gt; function convention and &lt;code&gt;Default&lt;/code&gt; trait in Rust, except that the instance is provided through the implicit &lt;code&gt;self&lt;/code&gt; value rather than being returned.&lt;/li&gt;
&lt;li&gt;Default initializers are generated for each struct/class type (as long as all fields have default values and the type doesn't provide at least one explicit initializer).&lt;/li&gt;
&lt;li&gt;Memberwise initializers are also generated for each struct/class type (if they don't provide at least one explicit initializer).  They are the same as the default initializers except they take parameters with labels matching the names of the fields of the type.&lt;/li&gt;
&lt;li&gt;All properties of a value must be initialized by the time the initializer returns.  Properties may be initialized either in the property declaration itself, or in the code of the initializer.&lt;/li&gt;
&lt;li&gt;Initializers can call other initializers of the same type, to reduce duplication of code.&lt;/li&gt;
&lt;li&gt;Classes can have special initializers called "convenience initializers" (declared by adding the &lt;code&gt;convenience&lt;/code&gt; keyword before the initializer).  These are not used implicitly by subclasses, but can be used for direct initialization of values, as well as by other initializers.&lt;/li&gt;
&lt;li&gt;Superclass initializers are called from subclass initializers through the name &lt;code&gt;super&lt;/code&gt;, as in: &lt;code&gt;super.init()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Initializers that are not "convenience" are referred to as "designated initializers".&lt;/li&gt;
&lt;li&gt;Swift enforces rules about how initializers can call other initializers in the inheritance hierarchy of a class:

&lt;ul&gt;
&lt;li&gt;Designated initializers must call a designated initializer from its immediate superclass, if any.&lt;/li&gt;
&lt;li&gt;Convenience initializers must call another initializer from the same class.&lt;/li&gt;
&lt;li&gt;Convenience initializers must "ultimately" (either directly or through another convenience initializer) call a designed initializer.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Swift enforces rules about property initialization within the inheritance hierarchy of a class:

&lt;ul&gt;
&lt;li&gt;A designated initializer must ensure all non-inherited properties are initialized before calling a superclass designated initializer.&lt;/li&gt;
&lt;li&gt;A designated initializer cannot assign to an inherited property until after calling a superclass designated initializer.&lt;/li&gt;
&lt;li&gt;A convenience initializer must call a designated initializer before assigning to any property.&lt;/li&gt;
&lt;li&gt;An initializer cannot call any methods of the type, read any property values, or refer to &lt;code&gt;self&lt;/code&gt; as a value until after all superclass designated initializers have returned.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;An initializer can be marked fallable by using &lt;code&gt;init?&lt;/code&gt; instead of &lt;code&gt;init&lt;/code&gt; for the initializer name.

&lt;ul&gt;
&lt;li&gt;Such an initializer can use a &lt;code&gt;return nil&lt;/code&gt; statement to indicate that initialization failed.&lt;/li&gt;
&lt;li&gt;Calling such an initializer produces an optional value.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;By using &lt;code&gt;init!&lt;/code&gt; instead of &lt;code&gt;init?&lt;/code&gt;, an initializer produces an instance that is "implicitly unwrapped".&lt;/li&gt;
&lt;li&gt;A superclass can require all subclasses to provide an initializer of a certain form (parameters) by declaring its own initializer with that form and adding the &lt;code&gt;required&lt;/code&gt; keyword in front.&lt;/li&gt;
&lt;li&gt;Default property values can be specified as closure or function calls, in order for those default values to be computed during initialization.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://docs.swift.org/swift-book/LanguageGuide/Deinitialization.html"&gt;Deinitialization&lt;/a&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Swift supports "deinitializers" for classes (not structures or enums).  They are analogous to the &lt;code&gt;Drop&lt;/code&gt; trait in Rust.&lt;/li&gt;
&lt;li&gt;The deinitializer is declared like an initializer with no parameters, but with the name &lt;code&gt;deinit&lt;/code&gt; instead of &lt;code&gt;init&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://docs.swift.org/swift-book/LanguageGuide/OptionalChaining.html"&gt;Optional Chaining&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Optional chaining in Swift refers to using the question-mark (&lt;code&gt;?&lt;/code&gt;) operator in an expression that calls a property or method of an optional value.  It's analogous to &lt;code&gt;Option::map&lt;/code&gt; or &lt;code&gt;Option::and_then&lt;/code&gt; in Rust.&lt;/p&gt;

&lt;p&gt;For example, in Swift::&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;result&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;SomeType&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;property&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;change&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is analogous to the following in Rust:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;SomeType&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="nf"&gt;.property&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nn"&gt;Property&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;change&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The way to think of it is the call chain consists of intermediate optional values, and stops if at any point an intermediate value is &lt;code&gt;nil&lt;/code&gt; (analogous to &lt;code&gt;None&lt;/code&gt; in Rust).&lt;/p&gt;

&lt;p&gt;Optional chaining which ends in an assignment results in that assignment only happening if the whole left-hand side expression is not &lt;code&gt;nil&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Swift automatically "flattens" the evaluated values in optional chaining, whereas in Rust it's up to the developer to select either &lt;code&gt;Option::map&lt;/code&gt; or &lt;code&gt;Option::and_then&lt;/code&gt; depending on whether an optional value or non-optional value is returned at any given point to avoid "nesting" options.  In other words, in Swift:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If a non-optional value is evaluated in an optional chain, it will become optional.&lt;/li&gt;
&lt;li&gt;If an optional value is evaluated in an optional chain, it remains optional (it does not become something like &lt;code&gt;Option&amp;lt;Option&amp;lt;T&amp;gt;&amp;gt;&lt;/code&gt; for example).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://docs.swift.org/swift-book/LanguageGuide/ErrorHandling.html"&gt;Error Handling&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Error handling was mostly already covered in The Basics. Essentially, Swift uses an "exception throw/catch" approach for handling errors, whereas Rust uses a "return success/failure" approach instead.&lt;/p&gt;

&lt;p&gt;Swift also supports a special code block called a "defer statement".  By adding the &lt;code&gt;defer&lt;/code&gt; keyword in front of a nested block of code, the compiler will execute the code inside the block just before the enclosing scope is exited.  This is useful for "cleanup" code that needs to be run in the event of an error being thrown causing the current scope to unwind.  Defer statements are executed in the reverse of the order in which they're written.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://docs.swift.org/swift-book/LanguageGuide/TypeCasting.html"&gt;Type Casting&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Two additional operators are needed in the Swift type system when dealing with classes, due to the nature of class inheritance:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;is&lt;/code&gt; operator is used to determine at runtime if a value is of a certain subclass type.  This is similar to &lt;code&gt;Any::is&lt;/code&gt; in Rust.

&lt;ul&gt;
&lt;li&gt;For example: &lt;code&gt;let isApple: Bool = (fruit is Apple)&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;as?&lt;/code&gt; and &lt;code&gt;as!&lt;/code&gt; operators are used to "downcast" a value to a subclass type.  The &lt;code&gt;as?&lt;/code&gt; operator returns an optional subclass value.  The &lt;code&gt;as!&lt;/code&gt; operator returns a subclass value or triggers a runtime error if the value isn't of the subclass type.  These are similar to &lt;code&gt;Any::downcast_ref&lt;/code&gt; in Rust.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://docs.swift.org/swift-book/LanguageGuide/NestedTypes.html"&gt;Nested Types&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Swift, unlike Rust, supports "nested types", which are types declared within the scope of another type.  They are merely syntactic sugar for the purpose of grouping types or limiting types used in implementations from being exposed in the public interface.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://docs.swift.org/swift-book/LanguageGuide/Extensions.html"&gt;Extensions&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Swift supports adding computed properties, methods, initializers, subscripts, nested types, and type protocol implementations to existing types through a special syntax.  The existing type name is prefixed with the &lt;code&gt;extension&lt;/code&gt; keyword and followed by a block containing the new properties, methods, etc. inside.  This is all syntactic sugar giving the appearance of extending a type, when in actuality the new functionality is added alongside the existing type.  It's similar to "extension traits" in Rust?&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://docs.swift.org/swift-book/LanguageGuide/Protocols.html"&gt;Protocols&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Protocols in Swift are analogous to "traits" in Rust.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Protocols are declared using the &lt;code&gt;protocol&lt;/code&gt; keyword, followed by the protocol name, followed by a block containing declarations of the requirements of any type implementing the protocol.&lt;/li&gt;
&lt;li&gt;Property requirements are specified like computed properties without statement blocks.&lt;/li&gt;
&lt;li&gt;Method requirements are specified like methods but without bodies.&lt;/li&gt;
&lt;li&gt;As with superclasses, protocols include the &lt;code&gt;required&lt;/code&gt; keyword with initializer requirements.&lt;/li&gt;
&lt;li&gt;Types implementing protocols list the protocols after the type name.  If more than one protocol is implemented, they are all listed together separated by commas.&lt;/li&gt;
&lt;li&gt;Swift protocols can be used as types for values.  Such values are analogous to "trait objects" in Rust, allocated on the heap and always handled by reference-counted pointer, although Swift hides most of these details from the programmer.&lt;/li&gt;
&lt;li&gt;Generic types in Swift can conditionally conform to a protocol by listing constraints on the type, similar to trait bounds in Rust.&lt;/li&gt;
&lt;li&gt;Swift provides automatic ("synthesized") implementations of the &lt;code&gt;Equatable&lt;/code&gt;, &lt;code&gt;Hashable&lt;/code&gt;, and &lt;code&gt;Comparable&lt;/code&gt; protocols for types that adhere to certain rules specific to those protocols.

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Equatable&lt;/code&gt; — &lt;code&gt;PartialEq&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Hashable&lt;/code&gt; — &lt;code&gt;Hash&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Comparable&lt;/code&gt; — &lt;code&gt;PartialOrd&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Protocols can be restricted for use by classes only by inheriting from the special &lt;code&gt;AnyObject&lt;/code&gt; protocol.&lt;/li&gt;
&lt;li&gt;Protocol composition is a way to combine multiple protocol requirements for a value of some concrete type.  It's specified similarly to how multiple trait bounds are specified in Rust, except that &lt;code&gt;&amp;amp;&lt;/code&gt; is used to separate multiple kprotocols rather than the &lt;code&gt;+&lt;/code&gt; used to separate multiple traits in Rust trait bounds.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;is&lt;/code&gt;, &lt;code&gt;as?&lt;/code&gt;, and &lt;code&gt;as!&lt;/code&gt; operators work with procotols the same way as they do for subclasses.&lt;/li&gt;
&lt;li&gt;For interoperability with Objective-C, Swift allows protocols marked with the &lt;code&gt;@objc&lt;/code&gt; attribute to contain "optional requirements", or specifications of properies or methods which may or may not be implemented for a type.

&lt;ul&gt;
&lt;li&gt;Such requirements have the keywords &lt;code&gt;@objc&lt;/code&gt; and &lt;code&gt;optional&lt;/code&gt; at the front.&lt;/li&gt;
&lt;li&gt;An "optionally required" method is essentially an "optional function".  The &lt;code&gt;?&lt;/code&gt; operator can be used in a call to such a method, to use "optional chaining" to test at runtime to see if the method is available on the value before calling it:  &lt;code&gt;someOptionalMethod?(someArgument)&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Default implementations for protocol requirements can be specified using property extensions.  This makes such requirements essentially "optional" for types to implement; if a type implements it, that implementation is used, otherwise the default implementation is used.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://docs.swift.org/swift-book/LanguageGuide/Generics.html"&gt;Generics&lt;/a&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;An extension declaration for a generic type does not repeat the declaration of the type parameters.&lt;/li&gt;
&lt;li&gt;Generic associated types are declared in protocols using the &lt;code&gt;associatedtype&lt;/code&gt; keyword (in Rust you would just use the &lt;code&gt;type&lt;/code&gt; keyword). When implementing such a protocol, the associated type is inferred, unlike in Rust where you have assign it to the associated type placeholder in the implementation of the generic.

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;struct Stack&amp;lt;Element&amp;gt;: Container&lt;/code&gt; — &lt;code&gt;type Item = Element&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Associated type constraints are declared along with the associated type in the protocol, whereas in Rust you would apply the constraints in the implementation blocks or methods where those constraints are needed.&lt;/li&gt;
&lt;li&gt;Swift supports type constraint specifications on generic type parameters using &lt;code&gt;where&lt;/code&gt; clauses similar to Rust; however, the clauses can specify that types be equal, in addition to that types implement certain protocols.  The equivalent of requiring type equality in Rust is to use "associated type bindings".  For example:

&lt;ul&gt;
&lt;li&gt;Swift: &lt;code&gt;func foo&amp;lt;I: Iterator&amp;gt;(it: I) where I.Item == Foo&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Rust: &lt;code&gt;fn foo&amp;lt;I&amp;gt;(it: I) where I: Iterator&amp;lt;Item=Foo&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Extensions of generics may also add type constraint specifications of their own.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://docs.swift.org/swift-book/LanguageGuide/OpaqueTypes.html"&gt;Opaque Types&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;some&lt;/code&gt; keyword in Swift is used similarly to the &lt;code&gt;impl&lt;/code&gt; keyword, particularly in the return value position, to stand for "some type that implements a protocol (trait)" where the compiler can determine the actual concrete type:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;-&amp;gt; some Shape&lt;/code&gt; — &lt;code&gt;-&amp;gt; impl Shape&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://docs.swift.org/swift-book/LanguageGuide/AutomaticReferenceCounting.html"&gt;Automatic Reference Counting&lt;/a&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Weak references: &lt;code&gt;weak var tenant: Person?&lt;/code&gt; — &lt;code&gt;let mut tenant: Weak&amp;lt;Person&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;An "unowned reference" has the &lt;code&gt;unowned&lt;/code&gt; keyword before the declaration of a non-optional value.  Such a reference does not participate in automatic reference counting, but unlike a weak pointer it's not "optional" (cannot be &lt;code&gt;nil&lt;/code&gt;).  A "dangling" unowned reference causes a runtime error if accessed. There is no clear analogue to this in Rust; &lt;code&gt;*const T&lt;/code&gt;, and &lt;code&gt;*mut T&lt;/code&gt; come close to realizing this concept but don't match precisely?&lt;/li&gt;
&lt;li&gt;An "unowned optional reference" is the same as an "unowned reference" except the value is an optional type.  This makes it analogous to a &lt;code&gt;*const T&lt;/code&gt; or &lt;code&gt;*mut T&lt;/code&gt; in Rust?&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://docs.swift.org/swift-book/LanguageGuide/MemorySafety.html"&gt;Memory Safety&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Essentially, Swift does a limited form of "borrow checking" by detecting "memory conflicts" where there is some overlap (multi-borrow) in referencing values.  The following examples are provided by the book:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In-out parameters: passing a value for an &lt;code&gt;inout&lt;/code&gt; parameter to a function which accesses the same value&lt;/li&gt;
&lt;li&gt;"Self": passing a value for an &lt;code&gt;inout&lt;/code&gt; parameter to a method of the same value&lt;/li&gt;
&lt;li&gt;Passing two parts of the same tuple as &lt;code&gt;inout&lt;/code&gt; parameters to a function&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://docs.swift.org/swift-book/LanguageGuide/AccessControl.html"&gt;Access Control&lt;/a&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A "module" in Swift is a "crate" in Rust.&lt;/li&gt;
&lt;li&gt;Access levels are relative to module and source file.&lt;/li&gt;
&lt;li&gt;Swift has finer-grained access control than Rust:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;open&lt;/code&gt; — like &lt;code&gt;public&lt;/code&gt;, but allows subclassing in other modules.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;public&lt;/code&gt; — essentially &lt;code&gt;pub&lt;/code&gt; in Rust&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;internal&lt;/code&gt; — essentially &lt;code&gt;pub(crate)&lt;/code&gt; in Rust&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;fileprivate&lt;/code&gt; — analogous to &lt;code&gt;pub(self)&lt;/code&gt; in Rust, if in the top-level module for a source file.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;private&lt;/code&gt; — analogous to &lt;code&gt;private&lt;/code&gt; in C++; not applicable to Rust.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://docs.swift.org/swift-book/LanguageGuide/AdvancedOperators.html"&gt;Advanced Operators&lt;/a&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;&amp;amp;+&lt;/code&gt; — &lt;code&gt;{integer}::overflowing_add&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;amp;-&lt;/code&gt; — &lt;code&gt;{integer}::overflowing_sub&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;amp;*&lt;/code&gt; — &lt;code&gt;{integer}::overflowing_mul&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;static func +&lt;/code&gt; — &lt;code&gt;std::ops::Add&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;static prefix func -&lt;/code&gt; — &lt;code&gt;std::ops::Neg&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;static postfix func !&lt;/code&gt; is for postfix operators (no equivalent in Rust?).&lt;/li&gt;
&lt;li&gt;You can define custom operators in Swift, such as: &lt;code&gt;static prefix func +++&lt;/code&gt;.

&lt;ul&gt;
&lt;li&gt;For custom infix operators, you can define what operator precedence group they should belong to like this: &lt;code&gt;infix operator +-: AdditionPreference&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Closing Thoughts
&lt;/h2&gt;

&lt;p&gt;If you made it here by reading through the entire post,&lt;br&gt;
  🌟Congratulations!🌟&lt;br&gt;
If you jumped around and just read the sections that interested you, that's fine too.  Thanks for reading, and please let me know in the comments if there are any mistakes, things I missed, and ways I can improve.&lt;/p&gt;

</description>
      <category>swift</category>
      <category>rust</category>
    </item>
    <item>
      <title>Git: Renaming the "master" branch</title>
      <dc:creator>Rhymu</dc:creator>
      <pubDate>Tue, 16 Jun 2020 07:02:23 +0000</pubDate>
      <link>https://forem.com/rhymu/git-renaming-the-master-branch-137b</link>
      <guid>https://forem.com/rhymu/git-renaming-the-master-branch-137b</guid>
      <description>&lt;p&gt;Recently, the CEO of GitHub &lt;a href="https://twitter.com/natfriedman/status/1271253144442253312"&gt;responded to a Tweet&lt;/a&gt; about renaming default Git branches from &lt;code&gt;master&lt;/code&gt; to &lt;code&gt;main&lt;/code&gt;, saying, "It's a great idea and we are already working on this!"  Now, you may or may not believe that terms such as &lt;code&gt;master&lt;/code&gt;, &lt;code&gt;slave&lt;/code&gt;, and &lt;code&gt;blacklist&lt;/code&gt; are racially-charged or have undesirable connotations nowadays.  However, I hope we can all agree that it's always good to have the know-how and the tools to be able to change the names we use.  That goes for branch names as well.&lt;/p&gt;

&lt;p&gt;Renaming Git branches isn't that difficult.  However, it gets a tad harder when you want to rename it both locally &lt;em&gt;and&lt;/em&gt; upstream.  It gets even trickier for the &lt;em&gt;default&lt;/em&gt; branch, because of an interesting fact about Git that I consider to be a design flaw:  &lt;strong&gt;Git has no built-in method of changing the default branch remotely.&lt;/strong&gt;  It's up to the developer to go to where it's hosted (GitHub, GitLab, Bitbucket, etc.) to change it.&lt;/p&gt;

&lt;p&gt;Therefore, I spent some time today coming up with a procedure for renaming branches, which I then used to rename around 60 public and private Git repositories that I maintain.  I thought I would post a write-up here about it, in case anyone is interested or needs to learn it themselves:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Rename your local branch.&lt;/p&gt;

&lt;p&gt;git branch -m master main&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Push renamed branch upstream and set remote tracking branch.&lt;/p&gt;

&lt;p&gt;git push -u origin main&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Log into the upstream repository host (GitHub, GitLab, Bitbucket, etc.) and change the "default branch".&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Delete the old branch upstream.&lt;/p&gt;

&lt;p&gt;git push origin --delete master&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Update the upstream remote's HEAD.&lt;/p&gt;

&lt;p&gt;git remote set-head origin -a&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That's covers it on &lt;em&gt;your&lt;/em&gt; end and where your repository is hosted.  Now what do you have to do if it's &lt;em&gt;someone else's repo&lt;/em&gt; which renamed a branch, and you're left holding a "dangling" reference (so to speak) to a remote branch that no longer exists?&lt;/p&gt;

&lt;p&gt;If you know the branch was renamed, there's nothing to fear.  The following steps will get you back on track:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Fetch the latest branches from the remote.&lt;/p&gt;

&lt;p&gt;git fetch --all&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Update the upstream remote's HEAD.&lt;/p&gt;

&lt;p&gt;git remote set-head origin -a&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Switch your local branch to track the new remote branch.&lt;/p&gt;

&lt;p&gt;git branch --set-upstream-to origin/main&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Rename your branch locally.&lt;/p&gt;

&lt;p&gt;git branch -m master main&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That's it!  Note that it gets much simpler if you have no remote/upstream.  It's just &lt;code&gt;git branch -m master main&lt;/code&gt; to rename a branch.  The other commands are all concerned about matching local branches to remote ones (via tracking branches) and updating the HEAD reference to point to the "default" branch.&lt;/p&gt;

&lt;p&gt;[EDIT: &lt;a class="mentioned-user" href="https://dev.to/eyqs"&gt;@eyqs&lt;/a&gt;
 made a neat tool to automate this process, including changing the default branch on GitHub so you don't have to do it by hand!  Of course, as always, use at your own risk, read the code yourself first to make sure you can trust it, etc.  &lt;a href="https://eyqs.ca/tools/rename/"&gt;eyqs.ca/tools/rename/&lt;/a&gt;]&lt;/p&gt;

</description>
      <category>git</category>
      <category>github</category>
    </item>
    <item>
      <title>C++ Developer Learning Rust!</title>
      <dc:creator>Rhymu</dc:creator>
      <pubDate>Wed, 10 Jun 2020 05:33:03 +0000</pubDate>
      <link>https://forem.com/rhymu/c-developer-learning-rust-2oal</link>
      <guid>https://forem.com/rhymu/c-developer-learning-rust-2oal</guid>
      <description>&lt;p&gt;Hello, dev.to community!  This is my first post, so please be kind.  This will be about my experience and impressions today as a C++ developer learning the Rust programming language.&lt;/p&gt;

&lt;p&gt;I've been streaming on Twitch for a couple years now.  Most of the time, I'm doing a combination of work on an indie game dev project and showing the process of software development to others, all while interacting with viewers and trying to answer their questions.  That includes working through problems, building up designs, and often learning new things myself.&lt;/p&gt;

&lt;p&gt;Today I had a somewhat different stream.  Over the course of the last few months, many viewers have either recommended or asked about Rust, a relatively new programming language I had only heard rumors about.  So I decided I would learn Rust.  Live, on stream.  I had a blast!&lt;/p&gt;

&lt;h2&gt;
  
  
  First Day of Learning
&lt;/h2&gt;

&lt;p&gt;I began the day with a vague impression of what Rust is, and a handful of links.  &lt;a href="https://twitch.tv/cmgriffing"&gt;Chris Griffing&lt;/a&gt;, who I look up to as an expert web-dev guru, gave me a lot of encouragement, as well as some links to start with, including a link to the &lt;a href="https://doc.rust-lang.org/stable/book/"&gt;Rust Book&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In four hours, I made it through the first three chapters of the book.  So far, I like the book, and the language.  I could have probably gone a bit faster, but I had a lot of people looking at it with me, and they had many questions, ideas, and suggestions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Things about Rust that Stand Out From a C++ Perspective
&lt;/h2&gt;

&lt;p&gt;I've been using C++ for a long time.  Learning a new programming language, once you already know a few of them, is mostly about recognizing what's similar and what's different between what you already know and what you're learning.  So I thought it might be interesting and perhaps useful for others if I took notes as I learned Rust, especially pointing out the things that are distinctly different between C++ and Rust.&lt;/p&gt;

&lt;p&gt;So far, these are my observations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Although Rust is a compiled language and so feels like C++, it has a nice packaging system (&lt;code&gt;cargo&lt;/code&gt;) that reminds me strongly of Node's &lt;code&gt;npm&lt;/code&gt; tool.&lt;/li&gt;
&lt;li&gt;Variables are immutable by default but adding &lt;code&gt;mut&lt;/code&gt; makes them mutable.  This is exactly opposite of C++, where you need &lt;code&gt;const&lt;/code&gt; to make something immutable. (Apparently, &lt;code&gt;const&lt;/code&gt; is the evil twin of &lt;code&gt;mut&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;The type binding syntax is different, but has the same concept in C++.  Without any type annotation, it's like using &lt;code&gt;auto&lt;/code&gt; in C++.  Adding a type annotation is like using an explicit type in C++, except the type annotation comes &lt;em&gt;after&lt;/em&gt; the variable name, not before (and there's also a colon to deal with).&lt;/li&gt;
&lt;li&gt;Creating a new object involves calling a special function "new" on the type (example: &lt;code&gt;String::new()&lt;/code&gt;  rather than &lt;code&gt;new String&lt;/code&gt; or &lt;code&gt;String()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;match&lt;/code&gt; is like &lt;code&gt;switch&lt;/code&gt; but:

&lt;ul&gt;
&lt;li&gt;It lacks parenthesis around its argument (which is true of Rust expressions in general).&lt;/li&gt;
&lt;li&gt;Instead of the &lt;code&gt;case&lt;/code&gt; keyword, we use arrows between the case and the code for the case (Rust calls the code an "arm").&lt;/li&gt;
&lt;li&gt;It is a first-class expression; in other words, it returns a value, similar to a function; each arm should "return" something, as in evaluate to an expression whose value will be returned if that arm is chosen.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;cmp&lt;/code&gt; is a method you can use on anything that can be compared, and it returns an enumerated type &lt;code&gt;Ordering&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;We can shadow variables in the same scope (you can do this in C++ but you have to create a inner scope to do it).

&lt;ul&gt;
&lt;li&gt;A common pattern is to use shadowing to "update" an immutable variable.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Integer overflow is checked and a panic is generated on overflow, but only in &lt;em&gt;debug&lt;/em&gt; builds.&lt;/li&gt;
&lt;li&gt;The "char" type is actually 4 bytes and contains the character's Unicode code point, rather than being simply ASCII or implying some kind of encoding like UTF-8.&lt;/li&gt;
&lt;li&gt;The "tup" (tuple) type can be &lt;em&gt;destructured&lt;/em&gt; similar to array/object destructuring in ES6.

&lt;ul&gt;
&lt;li&gt;You can also index a tuple by literal integer index (i.e. "x.0").&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Array type annotations are a bit alien:

&lt;ul&gt;
&lt;li&gt;[i32; 5] means array of five 32-bit integers.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Alternate array initialization syntax which looks similar to array type annotation:

&lt;ul&gt;
&lt;li&gt;[3; 5] means initialize to be a five-element array, where each element is initialized to have the value 3.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Blocks are also expressions!  Sweet!!&lt;/li&gt;
&lt;li&gt;Function return type specifications are like they are in C++ if you use the &lt;code&gt;auto&lt;/code&gt; keyword (only in Rust you don't need the &lt;code&gt;auto&lt;/code&gt; obviously).&lt;/li&gt;
&lt;li&gt;Rust is a bit (too much, in my opinion) opinionated about a few things:

&lt;ul&gt;
&lt;li&gt;Functions and variables should use snake_case.&lt;/li&gt;
&lt;li&gt;The last line of a function should not be a statement, but just an expression that becomes the return value implicitly.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Rust's idea of "void" is an empty tuple () &amp;lt;-- neat&lt;/li&gt;
&lt;li&gt;The "for" loop is like C++'s for-colon syntax, only using "in" instead of the colon (similar to Python?)&lt;/li&gt;
&lt;li&gt;Standard library defines a &lt;code&gt;Range&lt;/code&gt; type which lets you form sequence generators like this:  (0..4)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;match&lt;/code&gt; and &lt;code&gt;if&lt;/code&gt; are expressions too!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That was it for my first day of learning.  I'm really looking forward to the second day!  You can bet I will live stream the experience too.&lt;/p&gt;

</description>
      <category>rust</category>
      <category>learning</category>
      <category>programming</category>
      <category>twitch</category>
    </item>
  </channel>
</rss>
