Talks
Summarized using AI

Ruby ❤️ Rust

by Matias Korhonen

In this video titled "Ruby ❤️ Rust," Matias Korhonen provides a quick introduction to Rust programming and its integration with the Ruby ecosystem, specifically focusing on Rust-based gem extensions. The presentation, delivered at the Helsinki Ruby Brigade meet-up on May 10, 2023, highlights the key features of Rust and practical examples of using Rust within Ruby applications.

Main Topics Covered:
- Introduction to Rust:
- Rust is described as a multi-paradigm, high-level programming language that emphasizes performance, type safety, and concurrency.
- The language ensures memory safety without reliance on garbage collection, promoting efficient memory management.

  • Rust Syntax Overview:

    • Comparisons are drawn to C-like languages, indicating familiarity for those with prior programming experience.
    • Basic syntax includes variable assignment, method calls, and return values, showcasing similarities and differences to Ruby.
  • Integrating Rust With Ruby:

    • A significant feature introduced into Bundler allows easy creation of Ruby gems with Rust-based native extensions.
    • Developers can use existing gem skeleton generators by including an EXT=rust parameter to facilitate this integration.
  • Example Files in Rust Setup:

    • The setup includes essential files such as Cargo.toml and lib.rs, necessary for configuring and writing Rust code.
    • The presenter demonstrates a 'Hello World' program that illustrates the integration between Ruby and Rust, utilizing the Magnus library for bindings.
  • Live Demonstrations:

    • Matias performs live coding demonstrations, showing how commands like bundle install and compiling Rust code works in practice.
    • Further examples include wrapping text using a high-performance Rust library, allowing new functionality within Ruby applications without the complexities of C.
  • Advantages and Disadvantages:

    • The main advantage of this approach is that developers can create native extensions without needing C language skills, thus reducing the barrier to entry.
    • However, the downside mentioned is the necessity of having the Rust toolchain installed for development unless cross-compiling the gem.
  • Additional Resources:

    • Matias encourages viewers to explore the original blog post on Bundler for further reading, along with documentation for Magnus and Rust itself.
    • He also mentions the Rustlings course for those interested in learning more about Rust programming.

In conclusion, this video offers valuable insights into how Rust can enhance Ruby’s capabilities, enabling developers to leverage Rust’s performance benefits while remaining within the Ruby programming environment. Matias Korhonen provides a succinct yet informative guide to integrating these technologies, making it useful for Ruby developers looking to expand their skillset into Rust.

For code used in the presentation, attendees are directed to Matias's GitHub.

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!
Explore all talks recorded at Helsinki Ruby Brigade
+1