Ruby & Rust
Ship safer, faster Ruby code by writing performance-critical parts in Rust.
rb-sys makes building Ruby extensions in Rust easier than C, with memory safety guaranteed at compile time.
See It In Action
# Ruby code
require "my_gem"
# Call Rust from Ruby
result = MyGem.process_data(large_dataset)
# => Processed 1M records in 0.2s
// Rust code (using Magnus high-level API)
use magnus::{function, prelude::*, Error, Ruby};
#[magnus::init]
fn init(ruby: &Ruby) -> Result<(), Error> {
let module = ruby.define_module("MyGem")?;
module.define_singleton_method("process_data", function!(process_data, 1))?;
Ok(())
}
fn process_data(data: Vec<String>) -> Result<String, Error> {
// Your fast, safe Rust code here
Ok(format!("Processed {} records", data.len()))
}
Why Choose Rust for Ruby Extensions?
Memory Safety
C extensions are notorious for segfaults and memory leaks. Rust eliminates entire categories of bugs:
- No segmentation faults
- No memory leaks
- No data races
- No buffer overflows
Production Success Stories
These gems trust rb-sys in production:
- wasmtime-rb - WebAssembly runtime
- lz4-flex-rb - LZ4 compression
- blake3-rb - BLAKE3 hashing
When to Use Rust
- CPU-intensive algorithms (parsing, compression, cryptography)
- Memory-intensive operations (data processing, image manipulation)
- System integration (FFI, hardware access, OS APIs)
- Concurrent operations (parallel processing, async I/O)
When to use Ruby
- Business logic and application flow
- Simple CRUD operations
- Glue code and integrations
How It Works
The Stack
- Your Ruby code calls methods like any other Ruby library
- Magnus provides an ergonomic, safe Rust API for Ruby objects
- rb-sys handles the low-level Ruby C API bindings
- Rust ecosystem - Use any crate from crates.io in your Ruby app!
Quick Comparison
Feature | C Extensions | Rust + rb-sys |
---|---|---|
Memory Safety | Manual, error-prone | Guaranteed by compiler |
Segfaults | Common in production | Impossible* |
Development Speed | Slow | Fast |
Ecosystem | C libraries only | 130,000+ crates on crates.io |
Learning Curve | High | Low |
Cross-compilation | Manual, platform-specific | Automated with Docker |
Platform Support
Supported Rubies
- Ruby 3.0+ (recommended)
- Ruby 2.6+ (minimum)
- TruffleRuby
- JRuby (experimental)
Supported Platforms
- Linux (x86_64, aarch64)
- macOS (Intel & Apple Silicon)
- Windows (MSVC)
- Cross-compilation to all platforms
Join the Community
- Slack - Get help and share experiences
- GitHub Issues - Report bugs or request features
- Examples - Learn from working code
- YouTube - Watch talks and tutorials
Get Started
License: Apache-2.0 OR MIT | Minimum Ruby: 2.6+ | Minimum Rust: 1.65+