A template for building React Native applications with Expo that integrate native Rust code via FFI (Foreign Function Interface). This template demonstrates how to bridge Rust with Swift (iOS) and Kotlin (Android) in an Expo-managed workflow.
- 🦀 Rust integration with iOS and Android
- 📱 Expo managed workflow
- 🔄 Hot reloading support
- 🛠 Pre-configured build scripts
- 📚 Example arithmetic module included
- 🎯 TypeScript support
Before you begin, ensure you have installed:
- Node.js (v14 or later)
- Rust (latest stable)
- Xcode (for iOS development)
- Android Studio (for Android development)
- Expo CLI
-
Create a new project using this template:
npx create-expo-app my-app --template expo-rust-ffi-template
-
Install dependencies:
cd my-app npm install -
Build native modules:
# For iOS npm run build:ios # For Android npm run build:android
-
Start the development server:
npm start
my-app/
├── modules/
│ └── rust_ffi/ # Shared Rust FFI implementation
│ ├── ios/ # iOS FFI bindings and libraries
│ │ └── arithmetic/
│ └── android/ # Android FFI bindings and libraries
│ └── arithmetic/
├── rust_core/ # Rust implementation
│ ├── modules/ # Individual Rust modules
│ │ └── arithmetic/ # Example arithmetic module
│ ├── uniffi-bindgen/ # UniFFI bindings generator
│ ├── generated/ # Generated bindings
│ └── Cargo.toml # Workspace configuration
├── scripts/ # Build scripts
│ ├── build_ios.sh
│ └── build_android.sh
├── ios/ # iOS project files
├── android/ # Android project files
└── App.tsx # Main application entry point
-
Create a new module in the Rust workspace:
cd rust_core/modules mkdir my_module cd my_module cargo init --lib
-
Add the module to
rust_core/Cargo.toml:[workspace] members = [ "modules/arithmetic", "modules/my_module", "uniffi-bindgen" ]
-
Implement your Rust code:
// rust_core/modules/my_module/src/lib.rs #[uniffi::export] pub fn add(a: i32, b: i32) -> i32 { a + b }
-
Generate bindings:
npm run build:ios # For iOS npm run build:android # For Android
import { NativeModules } from 'react-native';
const { Arithmetic } = NativeModules;
const result = await Arithmetic.add(5, 3); // Returns 8- Use Android Studio for editing Kotlin files
- Use Xcode for editing Swift files
- Rebuild native code after making changes
- Keep native and JavaScript interfaces in sync
- Test changes in the app before committing
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.