October 5th, 2024

Cursed Rust

The article "Cursed Rust" humorously explores quirks of the Rust programming language, including the `Copy` and `Clone` traits, if statements as place expressions, naming conventions, and the utility of the `&*` operator.

Read original articleLink Icon
Cursed Rust

The article "Cursed Rust" discusses some peculiarities and humorous aspects of the Rust programming language, highlighting features that can lead to unexpected behavior. It begins with the `Copy` and `Clone` traits, illustrating how they can diverge in implementation, as shown in the `OhNo` struct example. The author points out that this divergence is not considered a logic error according to the documentation. Another obscure feature mentioned is that if statements qualify as place expressions, a detail not widely recognized even in the official reference. The article also contrasts the use of "krate" versus "crate_" in naming conventions, questioning how many developers are familiar with the Rust style guide. Additionally, it touches on the use of reference variables with `Cell`, demonstrating how mutable references can be reborrowed. Finally, the author notes the practical utility of the `&*` operator, which, while seemingly trivial, serves important functions in Rust, such as invoking Deref and managing raw pointers. Overall, the piece combines humor with insights into the quirks of Rust, encouraging developers to explore these features more deeply.

- Rust's `Copy` and `Clone` traits can lead to unexpected behavior.

- If statements are considered place expressions, a little-known fact.

- There is a discrepancy in naming conventions between "krate" and "crate_".

- Rust allows reborrowing of mutable references using `Cell`.

- The `&*` operator has practical applications despite its simplicity.

Link Icon 5 comments
By @davidatbu - 7 months
I don't know if it's due to my familiarity with Rust, but I find none of the examples cursed or surprising, except the "&* is useful" one. Even for that one, I think the use case of "turning a raw pointer to a reference" actually requires unsafe, and so it's imo a bit misleading to omit that fact.

All this is unlike a language i work with in my day job (and hence one would expect that I'd not find surprising), and yet I find so much behavior utterly baffling: https://dev.to/ben/what-is-the-oddest-javascript-behavior-4d...

By @dabacaba - 7 months
In Rust, if expressions are not place expressions. In the example, arr_ref is a reference to a temporary, not to an array element. In Rust, it is possible to take an address of any expression, but taking an address of a value expression will produce an address of a temporary.
By @atoav - 7 months
#00F on black is really not a color combination anybody should use in any case where accessability matters.
By @desdenova - 7 months
Cursed article by someone who doesn't know Rust.