BoltFFI 0.32 changes how the macros expand, and existing crates need a few edits. Read the migration guide

Use this page with an LLM

Markdown

Quick Start

Build your first BoltFFI project in 5 minutes.

1. Create a new library

cargo new --lib mylib
cd mylib

2. Configure Cargo.toml

[package]
name = "mylib"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["staticlib", "cdylib"]

Install BoltFFI:

cargo add boltffi

3. Write your Rust code

Replace src/lib.rs:

use boltffi::{data, export};

boltffi::scaffolding!();

#[data]
#[derive(Clone, Copy)]
pub struct Point {
    pub x: f64,
    pub y: f64,
}

#[export]
pub fn add(a: i32, b: i32) -> i32 {
    a + b
}

#[export]
pub fn greet(name: &str) -> String {
    format!("Hello, {}!", name)
}

#[export]
pub fn distance(a: Point, b: Point) -> f64 {
    ((b.x - a.x).powi(2) + (b.y - a.y).powi(2)).sqrt()
}

4. Build and generate bindings

# Initialize config
boltffi init

# Build for iOS
boltffi build ios --release

# Generate Swift bindings
boltffi generate swift

# Package as XCFramework
boltffi pack ios

# Or build the NuGet package for .NET
boltffi pack csharp

5. Use from Swift

let sum = add(a: 2, b: 3)
print("2 + 3 = \(sum)")  // "2 + 3 = 5"

let message = greet(name: "World")
print(message)  // "Hello, World!"

let p1 = Point(x: 0, y: 0)
let p2 = Point(x: 3, y: 4)
print("Distance: \(distance(a: p1, b: p2))")  // "Distance: 5.0"

6. Use from C#

int sum = MyLib.Add(2, 3);
Console.WriteLine($"2 + 3 = {sum}");

string message = MyLib.Greet("World");
Console.WriteLine(message);

Point p1 = new Point(0.0, 0.0);
Point p2 = new Point(3.0, 4.0);
Console.WriteLine(MyLib.Distance(p1, p2));

7. Use from C

Add [targets.c] with enabled = true to boltffi.toml, then run boltffi pack c --experimental --deny-skipped.

#include <stdio.h>
#include "mylib.h"

int main(void) {
    printf("%d\n", mylib_add(2, 3));
    MylibString message = mylib_greet(mylib_string_view("World", 5));
    fwrite(message.ptr, 1, message.len, stdout);
    mylib_string_free(&message);
    MylibPoint start = {0.0, 0.0};
    MylibPoint end = {3.0, 4.0};
    printf("\n%g\n", mylib_distance(start, end));
    return 0;
}

The returned string belongs to the caller, so it needs mylib_string_free. The points contain only numbers and need no cleanup. See linking a C program for compiler commands.