My Experience Building a Rust Library — ⚡rustygram

Yong kang Chia
3 min readNov 22, 2023
Blazing fast telegram notifications with ⚡rustygram

I recently built my first Rust library called ⚡rustygram after being inspired by the thriving Rust ecosystem and the wonderful crates that existed.

⚡rustygram, a minimal and blazing-fast Telegram notification framework for Rust, simplifies the integration of Telegram messaging into Rust applications by abstracting the complexities of the Telegram API.

The Motivation

I was building a trading bot in Rust and needed an easy way to integrate Telegram notifications into it without too much overhead. The existing Rust Telegram bot crates did not fully fit my needs — they were either too heavyweight, lacked the right abstractions I wanted, or used outdated crates.

I wanted something minimal that was easy to integrate, had the right abstractions, and used modern Rust crates under the hood. That inspired me to build ⚡rustygram.

Logo for rustygram

What I Learned

Rust Crates: A Developer’s Delight

  • With cargo publish, releasing a crate is a breeze.
  • Rolling out new versions is straightforward and hassle-free.
  • The automatically generated documentation on Docs.rs is outstanding, particularly when it comes to docstring formatting.
  • Doc tests serve as a great tool for both documenting the code and keeping it up-to-date.

Design Patterns

  • Rust allows you to use both functional and object-oriented programming styles. I initially experimented with both but ultimately chose a more OOP approach for rustygram.
  • Encapsulating the API client and bot state into a struct allowed for a cleaner object-oriented design that was easy to use and simulated the real Telegram bot behavior.
  • Functional patterns like closures can still wrap OOP components nicely in Rust. But for the core abstractions, OOP felt like the right choice.
  • Coming up with the right abstractions is critical yet challenging. I looked at other Telegram crates but none provided the simple yet powerful abstractions I wanted.
  • Rust’s strong static typing pushed me towards creating clean interfaces upfront. Once I had the core abstractions designed, the compiler helped enforce them throughout the code.
  • Overall, deciding on the right abstractions and OOP design took some iteration, but I’m happy with the API and flexibility that rustygram provides. Rust really shined in allowing me to try different designs safely.

Testing

  • Rust’s built-in documentation tests were invaluable. I leveraged them extensively to document and test rustygram’s modules and functions.
  • I initially overlooked integration testing. Once I added integration tests that actually interacted with the Telegram API, it uncovered many edge cases and gaps in the abstractions.
  • Setting up a continuous integration (CI) pipeline to run all tests on every commit revealed some flaky tests. CI helped me keep the test suite robust.
  • Overall, I learned to employ a diverse set of testing techniques beyond just unit tests. Rust’s test support made the iterative process very smooth. Tests were critical for verifying the reliability of the Telegram integrations.

Documentation

  • Creating comprehensive documentation with examples is critical for a good developer experience. I ensured the README clearly explains the key features, setup, and usage. The examples demonstrate the main use cases and APIs.
  • Doc comments use Markdown formatting for nice rendered documentation on docs.rs. I found docstring discipline crucial for keeping docs up-to-date.
  • The project badges prominently showcase the crate, docs, build status, and Telegram API coverage for quick scanning.
Badges
  • The setup instructions cover installing Rust, getting a Telegram bot token, configuring environment variables, and adding the crate dependency.
  • Testing and contribution guidelines answer common questions developers face. Providing this information upfront improves their experience.
  • I learned many conventions from other successful Rust projects for structure, tone, and formatting.

Try it Out!

Check out the crate on crates.io: https://crates.io/crates/rustygram

and the code on

GitHub: https://github.com/ExtremelySunnyYK/rustygram/tree/main

I welcome any feedback or contributions!

Building this first Rust library was an invaluable learning experience. I’m excited to continue improving ⚡rustygram and build more Rust libraries in the future.

--

--

Yong kang Chia

Blockchain Developer. Chainlink Ex Spartan Group, Ex Netherminds