Getting Started
This guide will walk you through creating a new Ruby gem with a Rust extension using rb-sys
.
Prerequisites
Before you begin, you'll need to have the following installed:
- Ruby (2.6+): https://www.ruby-lang.org/en/downloads/
- Rust (1.65+): https://www.rust-lang.org/tools/install
- Bundler:
gem install bundler
Create Your Gem
The easiest way to get started is by using the built-in bundle gem
command with the --ext=rust
option. This will generate a complete gem skeleton with a Rust extension ready for you to start building.
# Create a new gem with a Rust extension
bundle gem --ext=rust my_gem
cd my_gem
This will create a new directory called my_gem
with the following structure:
my_gem/
├── Gemfile # Ruby dependencies
├── my_gem.gemspec # Gem metadata
├── Rakefile # Build tasks
├── ext/my_gem/ # Rust extension
│ ├── Cargo.toml # Rust dependencies
│ ├── extconf.rb # Build configuration
│ └── src/
│ └── lib.rs # Rust code
└── lib/
└── my_gem.rb # Ruby wrapper
Build and Compile
To compile the Rust extension, you can use the provided Rake task:
# Install dependencies and compile the extension
bundle install
bundle exec rake compile
This will:
- Install any Ruby dependencies from the
Gemfile
. - Run the
extconf.rb
script, which creates aMakefile
for building the Rust extension. - Compile the Rust code in
ext/my_gem/src/lib.rs
into a shared library. - Copy the compiled library into the
lib/my_gem/
directory, so it can be loaded by Ruby.
Next Steps
Now that you have a working gem with a Rust extension, you're ready to start building!
- Quick Start Guide: A step-by-step tutorial to build your first real extension.
- Core Concepts: Learn about how
rb-sys
works under the hood.