Inline Const: The Borrow Checker's New BFF
Hello Rustacean! Welcome to another edition of the Rust Bytes newsletter. In this issue, we'll shine a spotlight on an amazing Rust project, present our spot-the-bug challenge, and share some incredible links of the week.
Welcome to Issue 20!
The Main Thing
Rust's Inline Const Reaches Stable Status! 🎉
The long-awaited feature of inline const expressions and patterns has finally been stabilized. Inline const will allow you to define and use constant values directly within expressions, eliminating the need for separate declarations.
Here's an example:
Traditional Const
const PI: f64 = 3.14159;
let area = PI * radius * radius;
Inline Const
let area = const { 3.14159 * radius * radius };
In the second example, we directly embed the value of PI (3.14159) within the calculation for the area, instead of declaring it separately. This eliminates the need for the PI constant and makes the code more compact.
This seemingly small change has significant implications:
Improved Readability: Code becomes more self-explanatory with constant values directly embedded.
Enhanced Performance: The compiler can optimize constant calculations at compile time, potentially leading to faster execution.
Simplified Code: Eliminates the need for separate constant declarations, reducing boilerplate code.
This feature is anticipated to land on the upcoming version of Rust 1.79.0, or try it out on Rust nightly.
You can check out the PR on GitHub.
Spot the Bug
Identify why the code fails to compile and suggest a way to fix it.
Share your solution in the comments below for a chance to be featured in our next newsletter.
Project Spotlight
Leptos
Leptos is a full-stack isomorphic Rust web framework that's build for speed and ease of use. Leptos uses fine-grained reactivity to build those smooth, declarative user interfaces you crave.
Here are its key features:
Leptos is full-stack versatile and it handles both client-side rendering and server-side magic.
Isomorphic awesomeness which lets you write code seamlessly across client and server, no need for multiple personalities.
Web-first philosophy built on the web's foundation, Leptos speaks its language fluently.
Reactive to the core: say goodbye to sluggish updates, Leptos keeps your UI blazing fast with its finely tuned reactivity system.
And the best part? Leptos is open-source on GitHub.
Awesome links of the week
A member of the LogLog Games team wrote a must-read critic on Rust as a game development language. Link.
The Mozilla team ported their cross-platform crash reporter GUI application to Rust and couldn't wait to blog about it. Well, at least they have a good excuse to jam 'written in Rust' on it. Link.
John Viega's "C Isn’t a Hangover; Rust Isn’t a Hangover Cure" will leave you chuckling over whether Rust is the ultimate solution, he argues that in the end, the best language, like the perfect hangover cure, depends on the specific situation. Link.
Nicolas Fränkel, explored the issues of error handling in Rust. The ? operator, though helpful, can get messy. Link.
Niko Matsakis wrote about the limitations of the current ?Sized notation in Rust and proposes a new approach using dedicated traits for handling type size categories. Link.
Lilos hit a major milestone and its now v1.0 stable release. Lilos is a tiny embedded operating system that uses async Rust to allow complex multitasking. Cheers to Cliff L. Biffle and the team 🥂 . Link.
Anshul Sanghi authored a comprehensive handbook on Rust procedural macros on the Freecodecamp blog. Link.
Robert Balicki joined Allen for an engaging discussion about Isograph, a dynamic Rust-powered framework tailored to improve React app development. 🎙️. Link.
Joe Neeman wrote about re-implementing the Nix protocol in Rust. Link.
Milos authored an in-depth account of his journey in developing conversational bots using Go and Rust. Link.
Spot The Bug: Solution
The Bug: Infinite Recursion
The factorial function recursively calls itself but forgets to multiply the result by n - 1 in the recursive case, leading to infinite recursion.
Solution:
Add the multiplication by n within the recursive case:
```
fn factorial(n: u32) -> u32 {
if n == 0 {
1
} else {
n * factorial(n - 1)
}
}
```
You can play with the code on Rust Playground.
Before You Go
To support the newsletter:
❤️ Recommend the newsletter to your friends: it really helps.
💸 Sponsor the newsletter.
🤳 Check us out on our socials: X, Rustaceans Publication.
📨 Contact us through rustaceanseditors@gmail.com feedback is welcome.
That's all for now, Rustaceans! Until next week, have a productive week ahead.