From the person that brought you calling Rust from Swift comes the thrilling[1], action[2]-packed sequel: calling Swift from Rust! For a recent project, I found myself needing to call into Swift from a Rust project (on both macOS and Linux) and so am documenting here in case you, too, are in this unenviable situation.
rust posts
Subscribe to just rust posts via RSS.
Part 12: Typed Variables
Hi. It’s been a while. Though the pace of blog posts fell off a cliff last year[1], I’ve continued working on my toy programming language on and off.
Using lol-html (or any Rust crate) in Swift
I recently started building a new iOS app and found myself with a need to parse HTML in order to extract some information. My goto tool for this in the past has been SwiftSoup. In this app, I have to deal with larger documents than I’d used it for previously, and unfortunately, its performance leavse something to be desired. Much of the issue comes from the fact that I only want to extract the first paragraph of a document, but SwiftSoup always needs to parse the entire thing—for large documents, potentially a lot of unnecessary work[1]. And, as far as I could find, there are no streaming HTML parsers written in Swift. One I did find, however, was CloudFlare’s lol-html. It’s specifically designed for speed and low latency, exactly what I want. But it’s written in Rust.
Part 11: Lexical Scope
After adding variables, I added boolean values and comparison operators, because why not. With that in place, I figured it would be a good time to add if statements. Parsing them is straightforward—you just look for the if
keyword, followed by a bunch of stuff—so I won’t go into the details. But actually evaluating them was a bit more complicated.
Part 10: Variable Declarations
Now that the parser can handle multiple statements and the usage of variables, let’s add the ability to actually declare variables.