00:00:05.700
All right, so for those of you who don't know me, I'm Matias. I'm one of the people who has been hanging around these Ruby events for a few years. If you want to know more, my website address is on the slide. Today, I'm here to talk about Rust and how it fits into the Ruby ecosystem.
00:00:17.940
Let's start with a short introduction to what Rust actually is. To quote Wikipedia, it's a multi-paradigm, high-level, general-purpose programming language that emphasizes performance, type safety, and concurrency. Rust enforces memory safety, ensuring that all references point to valid memory without requiring the use of a garbage collector or reference counting, which are present in other memory-safe languages. In simpler terms, it's a programming language focused on performance, making it very hard to leak memory.
00:00:36.660
It looks something like this, which should feel quite familiar if you've ever programmed in a C-like language. You can assign variables, print things, and make method calls with both implicit and explicit returns. It's somewhat similar to Ruby, yet quite different as well. That concludes my short introduction to Rust, so now you all know about Rust. But how does Rust fit in with Ruby?
00:01:26.640
At the beginning of this year, a blog post was published, and this feature was merged into Bundler. It is now super easy to create a new Ruby gem with Rust-based native extensions. You can use the same gem skeleton generators you did before, but simply call it with the EXT equals rust parameter. You'll then get a Ruby gem with built-in support for Rust extensions.
00:01:49.560
There are a few interesting files that come with this setup. There’s a Cargo.toml file, which is basically your gem file for Rust. It includes a configuration file for the native extension and a lib.rs file where you can write your Rust code. For example, this 'Hello World' program contains some boilerplate code from a project called Magnus, which provides bindings between Ruby and Rust, allowing you to easily integrate Rust methods and data types into the Ruby ecosystem.
00:02:39.239
For instance, Magnus helps manage data types, so an integer of varying sizes from Rust will return an integer in Ruby, and booleans will get mapped to Ruby's true/false. Now, I decided to do a demo at this point, so wish me luck as I show you how it works.
00:03:00.060
Here's my awesome Rust program. As you can see, it's basically what we generated earlier, with some compiled code since I already compiled it today. I’ll run the usual bundle install command to set things up. After that, I’ll compile the Rust parts. Then, through Ruby, we can invoke this method, and of course, Rust prints out 'Hello World!' This is something that would never be possible in Ruby. If we look closely, you’ll notice that we have some regular Ruby gem setup, and the Rust code that prints to the console.
00:04:46.280
As a brief digression, you can also call Ruby from Rust, meaning you can embed Ruby code within your Rust program if needed. However, you cannot embed Ruby in Rust; that's something I tried, and I was quite disappointed that it doesn't work. But what you might want to consider is integrating a Rust library into your Ruby gem.
00:05:43.440
For example, I’ve chosen a very high-performance library that wraps text. I decided to do another demo instead of simply recording it, so you'll have to forgive me as I go through the setup again. I'll run bundle install and then compile again because the Rust code has changed. After compiling, I call a method with some text, and we see it wraps the text correctly. We can also use default values by passing nil; if we do, it utilizes a default of 80 characters.
00:07:11.940
To summarize, the advantage here is that you can write native extensions without needing to know C, which is great news for those of us who don't have a background in C. Also, you don’t have to worry about memory issues that arise when writing in C or similar languages. However, the main downside is that you need to have the Rust toolchain installed unless you cross-compile the gem for the platforms you use.
00:08:25.759
If you want to read more, there’s the original blog post from Bundler, along with the documentation for Magnus and Rust itself. I encourage you to check out the Rustlings course and the official Rust documentation. If you're interested in the code from this presentation, it's available on my GitHub. That's all, thank you!