Rust Events Revisited

Rust Events Revisited

Write Rust events with ease

I recently wrote an article about writing events in Rust. I must admit I wasn’t entirely satisfied with the solution I came to. My previous code used a clunky macro-capture pattern to define a signal and produced receivers that couldn’t easily transform the data like event receivers should be expected to do.

Thus, we’d have to write custom transformations for each of our receivers — not very ergonomic. I decided I could do better! What I came up with is ultimately declared like this:

Code that both declares and uses the variables for our emitter and receivers, emitter1, r1, and r2.

In this way, we can both declare and use the variables for our emitter and receivers, emitter1r1, and r2. We can use any names we want for our identifiers. By using repeating captures, we can have multiple receivers with variable functionality by pairing them with a unique closure.

Also note that the types you use don’t have to exist — we’re literally creating them through this macro. So when you declare MySignal<MySignalReceiver, MySignalData>, even though none of these identifiers exist yet, they’ll be created with the provided names via macro expansion.

This version seems more versatile and more ergonomic. I’ve also taken some of my commentators’ advice and changed my naming conventions to be a bit more descriptive.

I’m going to walk through my process from start to finish, both because I’ve changed a lot of code and because it’ll be easier for new readers to understand without having read my other Rust events article. And besides, this article contains what I believe to be a superior solution.


What is Geek Coin

What is GeekCash, Geek Token

Best Visual Studio Code Themes of 2021

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Rust Casting, Shadowing, Consts and Static

A mix of topics of casting, shadowing, constants and static variables inside the Rust Programming Language. This Rust programming language tutorial series is aimed at easing your training step by step.

Rust Zürichsee, April: Gerhard Bräunlich - PyO3 and Rust in action

This presentation was the first experiment with livestreaming of the Rust Zürisee meetup group in Switzerland. Gerhard is sharing parts of his story migratin...

"Rust Isn’t Afraid to be Imperfect as Long as we Ship something Useful"

Steve Klabnik is a member of the Rust core team, an active open-source contributor, and author of The Rust Programming Language, Rails 4 in Action, and Designing Hypermedia APIs books. In 2012 and 2016, we invited Steve to speak at the RailsClub (now RubyRussia) conference. Since then, Steve has been working on Rust a lot, did a lot of interesting things and we realized that we should definitely interview him once again!

Prevent Breaking Code Changes in Future Releases using `non exhaustive` enums in Rust

Hello everyone, recently I have come across a feature in Rust, known as non_exhaustive. It was introduced in Rust 1.40.0 . This attribute prevents source code-breaking changes in projects downstream.

Rust vs Go - Which Is More Popular?

Rust vs Go - Which Is More Popular - Go and Rust are two of the hottest compiled programming languages. I develop in Go full-time and love it, and I'm learning more about Rust recently - its an exc