cargo-mobile: Rust on mobile made easy!
The answer to “how do I use Rust on iOS and Android?”
For nearly 2 years, we’ve been perfecting a tool called cargo-mobile. It’s a framework-agnostic tool that generates all the boilerplate of a Rust mobile project, and is packed full of handy commands for building and running on mobile devices.
If you’d like to follow along, installation is easy:
cargo install --git https://github.com/BrainiumLLC/cargo-mobile
Note that cargo-mobile currently only works on macOS! If you shoot us a PR that adds support for Linux or Windows, we’ll shower you in heart emoji. Feel free to ask us for guidance if you’re interested!
Additionally, until Rust 1.49.0 is released, you can’t build for iOS unless you’re using the current beta.
To start a new project, you just need to run cargo mobile init
:
Just like that, you have a version of Bevy’s Breakout example that can run out of the box on desktop, iOS, and Android!
As usual, you can use cargo run
to run on desktop:
More excitingly, it’s easy to run on iOS and Android!
iOS
We provide a convenience command for opening your project in Xcode:
cargo apple open
From there, you can build your project like you normally would in Xcode! Just hit that Play button. It’ll work on Simulator, too.
You can even set breakpoints!
If you’re not a fan of opening Xcode, you can also run directly from the command line. Be warned that the output might contain some spurious errors; we’re still improving that! Also, you can’t currently use Simulator unless you’re running from Xcode.
cargo apple run
Whether you run from Xcode or the command line, you should ultimately be greeted by Breakout:
We provide several other helpful iOS commands, which are listed in our help info:
cargo apple
Android
Naturally, we also provide a convenience command for opening your project in Android Studio:
cargo android open
Once again, you just need to hit the Run button like you normally would.
Alternatively, you can skip Android Studio and run directly from the command line:
cargo android run
Either way, it should pop up on your connected device:
You can find more Android commands in our help info:
cargo android
Going forward
Be advised that Bevy’s mobile support is still very new, so the Breakout demo doesn’t work perfectly yet. They’re doing an amazing job nonetheless! We’ve been following their progress, and a lot of contributors worked very hard to make it happen.
We’d love to see PRs that add template packs for other engines and frameworks, so that cargo-mobile is convenient for anyone and everyone to use. We’d also love to show you a demo using Brainstorm, our own internal mobile-first game engine, but we’re not ready to open source it just yet!
If you have any questions, issues, or ideas, feel free to open an issue!