RFC 3325 Paves the Way for Controlled Use of Unsafe Attributes in Rust
Today's Issue: Zed Lands on Linux, Unveiling oha, and RustNL 2024 Highlights!
Hello Rustacean! Welcome to another edition of the Rust Bytes newsletter. In this issue, we'll discuss a recently approved RFC, spotlight an amazing Rust project, challenge you with a Quiz, and share some incredible links of the week.
Welcome to Issue 24!
THE MAIN THING
Rust RFC 3325: Unsafe Attributes Approved
Rust has a process called Request for Comments (RFC) for introducing changes to the language. Through this process, proposed changes are thoroughly reviewed and discussed. If approved, they're integrated into Rust.
These changes happen regularly, and today, we'll look into a recently approved RFC 3325 and its benefits for both the Rust language and programmers.
In Rust, using unsafe code requires specific blocks denoted by unsafe { ... }. Unsafe attributes provide a way to mark specific elements within the code (like functions or macros) as unsafe, instead of needing the entire surrounding block to be unsafe.
Current Pain Points:
Lack of Granularity: Currently, if a function relies on unsafe operations (even from external factors), the entire function needs to be wrapped in an
unsafeblock. This can be misleading for library users as it implies the entire function is unsafe, even if only specific parts are.Unclear Responsibility: With unsafe blocks, it's not always clear where the unsafety originates. Is it inherent to the function itself, or is it due to external dependencies or factors outside the library author's control?
Code Readability: Large unsafe blocks can make code harder to understand and maintain. Separating the actual unsafe parts with attributes can improve readability.
The solution is to introduce unsafe attributes that can be applied directly to functions or other elements. This clarifies where the unsafety originates and separates it from the code itself.
The debate on this RFC about the best syntax for these attributes, options included unsafe(name), unsafe name, and unsafe { name }. Parentheses (unsafe(...)) ultimately received the most support for its consistency with other attribute syntax in Rust.
Overall, unsafe attributes will provide a more granular and informative way to handle unsafe code in Rust. They’ll improve code clarity, responsibility allocation, and maintainability.
CHALLENGE: RUST QUIZ❓
How many times will the message 'Value not found' be printed?
A. 2 (times)
B. 4 (times)
C. 1
PROJECT SPOTLIGHT 💡
oha: The HTTP Load Generator That Makes Stress Testing Fun
Worried about server performance? oha is the stress testing tool you need to identify bottlenecks and optimize your system.
oha is a tiny Rust program inspired by rakyll/hey, but with a twist: it throws in a real-time TUI for good measure. Think of it as a stress test with a side of ASCII art.
Here's what oha brings to the table (or should we say, server):
Blazing Fast: Rust gives oha the speed it needs to pummel your server with requests. We're talking Usain Bolt on a sugar rush kind of fast. ⚡️
TUI-riffic: Watch those requests fly by in a mesmerizing display of characters. It's like the Matrix, but for load testing.
Highly Customizable: Tweak oha to fit your specific needs. Want to simulate a swarm of locusts hitting your API? No problem.
But oha isn't just a pretty face (well, okay, it doesn't have a face, but you get the idea). It's also packed with features:
Multiple connections, allowing you to let loose a horde of virtual users to overwhelm your server.
Rate Limiting: Don't go too ham. Control the flow of requests with ease.
Fancy Features like burst mode, dynamic URLs, keep-alive control – oha has it all.
Easy to use with just a few simple commands and you're good to go.
Ready to put your server through its paces? Check out oha GitHub repo.
AWESOME LINKS OF THE WEEK 🔗
Rust Foundation received a vital boost as Microsoft joins as Platinum member with $1 million Donation.
Adrien Cacciaguerra and Arthur Pastel took us on a wild ride through Rust v1.78, highlighting the performance impact of the 128-bit Memory Alignment Fix – it's like watching a coding sitcom unfold in real-time!
RustNL 2024 Conference wrapped up last week in the Netherlands, featuring exciting lightning talks. If you missed check them out Day 1, and Day 2.
Aria Beingessner's Pair Your Compilers At The ABI Café highlights a code analysis tool (abi-cafe), ensuring your programs can chat across languages like old friends at a party.
Zed the editor written in Rust, now compiles and runs on Linux thanks to open-source contributions and conquering windowing system/graphics API hurdles.
Nick Cameron made a list of the big changes that have happened in the Rust project since it’s 1.0 release 9 years ago (which feels like, eons ago in programmer years).
Ashwin Narayan wrote an article explaining how to use Rust to program an LED to blink on an STM32 microcontroller board.
Ashley Mannix blogged about Seq's (diagnostic database) space-saving trick: a custom disk-backed hashmap for efficiently storing indexes of ever-changing data like trace IDs.
Prossimo announced that Rustls has gained OpenSSL and Nginx compatibility.
Ruihang Xia wrote about Error Handling for Large Rust Projects - A Deep Dive into GreptimeDB's Practices.
CHALLENGE: SOLUTION
Correct Answer:
B. 4 (times)
The key to understanding the behavior here is unwrap_or's eagerness. Unlike what it might seem, it executes the provided closure in all cases, regardless of whether the Option value is Some or None.
For Some values the closure is executed, but the existing value within Some is returned. This might seem unnecessary, but it's important for consistency and potential side effects within the closure.
For None values the closure is crucial. It's executed to provide a default value and often performs side effects like printing the message "Value not found!" in this example.
For a deeper understanding of unwrap_or and its potential pitfalls, you can refer to the previous newsletter where we discussed the concept of safe unwrap_or usage in Rust.
You can play with the code on Rust Playground.
BEFORE YOU GO
To support the newsletter:
❤️ Recommend Rust Bytes to your friends: it really helps.
🤳 Check us out on our socials: X, Rustaceans Publication.
📨 Contact us through rustaceanseditors@gmail.com feedback is welcome.
💸 Sponsor the newsletter.
That's all for now, Rustaceans! Until next issue, have a productive week ahead.



