Say Goodbye to If-Let Hell (Finally!)
Today’s issue: Homebrew’s Slayer, Interpreting Visibility in Rust, and Warp’s Netflix-Inspired Series
Hello Rustaceans!
Welcome to another edition of the Rust Bytes newsletter.
In this issue, we’ll discuss the recently stabilized let chains feature, challenge you to parse a file and count unique users, spotlight an amazing Rust project, and share some incredible links of the week.
Here’s issue 64 for you!
THE MAIN THING
Say Goodbye to If-Let Hell (Finally!)
After a seven-year grind (yep, 7 years, 3 months, and 30 days, but who’s counting?), let chains are officially stabilized and are expected to land Rust v1.88. This is huge for the Rust 2024 edition, and we’re stoked to see cleaner, leaner code in the wild.
Let chains, introduced in RFC 2497, are all about making Rust’s control flow way smoother. They let you chain multiple let bindings and boolean checks with && in if and while statements.
Think less nesting, less boilerplate, and more “wow, this is actually readable.” If you’ve ever wrestled with gnarly if let pyramids, this one’s for you.
Let’s dive into a dead-simple example to show off the magic.
Old School - Nested if let Pain
Picture this: you’re writing a program to process user input that represents a person’s profile. Each profile is an Option holding a tuple of a name (string) and age (integer).
You want to check if:
The profile exists (is Some).
The name is non-empty.
The age is at least 18.
Here’s how you’d do it without let chains.
Look at that nesting! It’s like a code onion - peel one layer, and another if makes you cry. Problems? Tons of indentation, repetitive blocks for each condition, and else branches cluttering up the vibe. Your main logic gets buried under all that boilerplate. Not cool.
New Hotness - Let Chains to the Rescue
Now, with let chains, you can flatten that mess into something beautiful.
Now we have one if statement, no nesting, and a single else to handle all the sad paths. Let chains let you mix let patterns (like Some((name, age))) with boolean checks (like !name.is_empty()) using &&.
The diff below shows how let chains eliminate significant boilerplate. Look at all that cruft gone.
Okay, let’s keep it real. Sometimes you need to handle each case separately - like if you’re logging specific errors for invalid data. In those cases, nested if let might still be your go-to.
Let chains are awesome for streamlined happy paths, but they’re not a one-size-fits-all. Choose your weapon wisely.
Why I’m Hyped
Let chains are like “syntactic sugar” that tastes just right. They fit perfectly into Rust’s pattern-matching mojo, making your code feel both powerful and chill.
I’m pumped to see how you'll use them in your projects. This feature’s gonna be a staple in idiomatic Rust, mark my words. Or, you know, maybe it’ll just make us all smile a bit more while coding. Either way, win-win!
RUST CHALLENGE 🦀
Last week, we shared a Rust tip to help you save time and reduce keystrokes using cargo aliases.
Let's move on to this week’s challenge.
JSON Log Analyzer
You are provided with a JSON file containing log entries data. Your task is to read the file and count the number of unique users based on the user IDs.
You should return the count as a usize, handle file I/O errors, and malformed entries gracefully. If the file is empty or no matching actions are found, return 0.
The data in the JSON takes the form of the struct below.
Bonus points if you can make it blazingly fast.
You can find the JSON file here and the starter template here. Once completed, please share your solution and tag us either on X, BlueSky, Mastodon, or reply to this email.
PROJECT SPOTLIGHT 💡
RMK
RMK is a keyboard firmware for programmers and enthusiasts to create highly customizable, and efficient keyboards.
With support for a wide variety of microcontrollers and advanced functionalities like real-time keymap editing and wireless connectivity, RMK stands out as a solution for building modern mechanical keyboards.
RMK’s feature set that makes it a standout choice for keyboard enthusiasts:
Broad Microcontroller Compatibility - Powered by Embassy, RMK supports STM32, nRF52, RP2040, and ESP32, enabling flexibility in hardware selection.
Real-Time Keymap Editing - Built-in Vial support allows users to modify keymaps on-the-fly, even over Bluetooth, enhancing customization without reflashing.
Advanced Keyboard Features - RMK includes layer switching, media controls, system controls, and mouse emulation out of the box, catering to various use cases.
Wireless BLE Support - With auto-reconnection and multi-device pairing, RMK ensures seamless wireless performance on nRF52 and ESP32 microcontrollers, tested on nRF52840, ESP32C3, and ESP32S3.
Low Latency and Power Efficiency - RMK achieves 2ms wired and 10ms wireless latency, with the async_matrix feature enabling ultra-low power consumption for extended battery life.
Whether you’re a hobbyist or a seasoned programmer, RMK provides the tools to bring your keyboard vision to life.
RMK is open-source on GitHub at https://github.com/HaoboGu/rmk, and they got a cool logo too!
AWESOME LINKS OF THE WEEK 🔗
The brilliant minds behind Warp (you know, that incredibly intelligent terminal built with Rust!) have released another episode of their captivating "The Trip" Netflix-style video series this week.
Sam Lijin gave a lightning talk on Shipping Rust to Python, Typescript, and Ruby. Prepare for cross-language wizardry! [video]
Marc from Tweede golf wrote Does using Rust really make your software safer?, demonstrates how Rust's memory safety and testing capabilities eliminate exploitable errors and improve code reliability.
Mond wrote “Pipelining might be my favorite programming language feature”. You might just find yourself falling for it too!
Anshul Sanghi’s Dark Arts of Interior Mutability conjures up Rust’s “fearless concurrency,” playfully bending strict memory rules with Mutex, Atomic types, and UnsafeCell for thread-safe data magic. Don’t skip this gem!
Erik Steiger wrote about Generating 1 Million PDFs in 10 Minutes (using Rust on AWS Lambda). If you’re a performance purist, this one is for you.
Jakub Beránek’s article on Two ways of interpreting visibility in Rust discusses the two distinct approaches to using visibility in Rust - global and local - detailing their mechanics, trade-offs, and impacts on code structure.
Alexander Knott open-sourced spm, a Rust-based package manager for macOS. Homebrew, consider this your official "watch your back" notification!
Ali ghahremani’s article on We have polymorphism at home explains how to achieve polymorphism in Rust, by using enums, traits, macros, and different function names to handle varied input parameters for a connect function.
Angus Morrison wrote about Mastering hexagonal architecture in Rust. Think of it as your sassy, all-in-one guide to crafting Rust apps so flexible and future-proof, they’ll practically wink at tech debt and say, “Not today!”
Bonus:
RustConf call for proposals ends tomorrow, April 29th. Whether it’s a war story from production, or a creative use case, the community wants to hear from you. Submit here.
CodeCrafters: Become a Better Rust Engineer
CodeCrafters created amazing Rust courses that push your skills beyond the basics.
You’ll have fun building real-world projects from scratch, including Git, Docker, Redis, Kafka, SQLite, Grep, BitTorrent, HTTP Server, an Interpreter, and DNS.
The courses are self-paced, so you can learn at your own speed.
If you’re itching to level up your Rust skills, these courses are perfect for you.
Here’s what makes CodeCrafters stand out:
Learn by building projects that challenge you beyond just implementing CRUD features.
Strengthen your fundamentals by working on awesome low-level projects.
Get really good at reading and writing idiomatic Rust code.
Plus, take part in monthly contests for a chance to win exciting prizes.
You can get your CodeCrafters fees fully reimbursed through your corporate Learning & Development (L&D) budget.
Be sure to check with your employer about tapping into your L&D budget to save money and make this a no-brainer opportunity to level up your skills.
Don't take our word for it. See what others have to say. [affiliate]
SUPPORT RUST BYTES
You’re Rust Bytes’ biggest fans, and we love to see it.
Here’s how you can help spread the word:
❤️ Recommend Rust Bytes to your friends.
🤳 Connect with us on our socials: X, BlueSky, Mastodon, Publication.
☕️ Support our editors by buying us coffee.
📨 Email us at rustaceanseditors@gmail.com for sponsorship, feedback or ideas.
I started growing some flowers indoors, and I am impressed with the progress.
That's all for now, Rustaceans.
John & Elley.
I have an incel-like platonic love relationship with Rust. It fascinates me, it inspires me. It also overwhelms me.