Skip to main content

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:

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

  1. Your Ruby code calls methods like any other Ruby library
  2. Magnus provides an ergonomic, safe Rust API for Ruby objects
  3. rb-sys handles the low-level Ruby C API bindings
  4. Rust ecosystem - Use any crate from crates.io in your Ruby app!

Quick Comparison

FeatureC ExtensionsRust + rb-sys
Memory SafetyManual, error-proneGuaranteed by compiler
SegfaultsCommon in productionImpossible*
Development SpeedSlowFast
EcosystemC libraries only130,000+ crates on crates.io
Learning CurveHighLow
Cross-compilationManual, platform-specificAutomated with Docker
* When using safe Rust. Unsafe blocks require careful review.

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

Get Started


License: Apache-2.0 OR MIT | Minimum Ruby: 2.6+ | Minimum Rust: 1.65+