This project demonstrates a simple integration between Swift and Rust. The Swift app provides a user interface that calls Rust functions for basic operations.
- Swift App: Contains the iOS/macOS application with UI
- rust-core: Contains the Rust library with core functionality
- Xcode (latest version recommended)
- Rust toolchain (install with rustup)
- cbindgen (
cargo install cbindgen)
- Clone the repository
- Run the build script to compile the Rust library:
cd "Swift App" ./build-rust.sh
- Open the Xcode project
Swift App/AI Video Editor.xcodeproj - Configure the Xcode project to link with the Rust library:
- Set "Swift Compiler - General" > "Objective-C Bridging Header" to:
$(PROJECT_DIR)/AI Video Editor/AI-Video-Editor-Bridging-Header.h - Add to "Search Paths" > "Header Search Paths":
$(PROJECT_DIR)/AI Video Editor/Libs - Add to "Linking" > "Other Linker Flags":
-lrust_core - Add to "Search Paths" > "Library Search Paths":
$(PROJECT_DIR)/AI Video Editor/Libs
- Set "Swift Compiler - General" > "Objective-C Bridging Header" to:
- Add a Run Script build phase (before "Compile Sources") with:
"${PROJECT_DIR}/build-rust.sh" - Build and run the project in Xcode
The Swift app communicates with Rust through a C-compatible Foreign Function Interface (FFI). The process is:
- Rust functions are marked with
#[no_mangle]andextern "C"to make them callable from C - The C header file (
rust_core.h) defines the interface for these functions - Swift uses a bridging header to import the C functions
- The
RustCore.swiftfile wraps these C functions in a more Swift-friendly API
- Adding two numbers using Rust
- Greeting a user with a message from Rust
- Dividing two numbers with error handling
To add more functionality:
- Add new functions to the Rust library in
rust-core/src/lib.rs - Update the C header file in
rust-core/include/rust_core.h - Add Swift wrapper methods in
Swift App/AI Video Editor/RustCore.swift - Update the UI in
Swift App/AI Video Editor/ContentView.swift
- If you encounter "Library not found" errors, make sure the Rust library has been built and copied to the correct location
- If Swift can't find the Rust functions, check that the bridging header is correctly configured
- For linking errors, verify that the library search paths and linker flags are set correctly